Skip to content

Commit 3ac8543

Browse files
DEPR: undo deprecation of astype(int64) for datetimelike values (#45449)
1 parent 91d3f0b commit 3ac8543

File tree

15 files changed

+37
-117
lines changed

15 files changed

+37
-117
lines changed

doc/source/whatsnew/v1.3.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ Other Deprecations
811811
- Deprecated allowing scalars to be passed to the :class:`Categorical` constructor (:issue:`38433`)
812812
- Deprecated constructing :class:`CategoricalIndex` without passing list-like data (:issue:`38944`)
813813
- Deprecated allowing subclass-specific keyword arguments in the :class:`Index` constructor, use the specific subclass directly instead (:issue:`14093`, :issue:`21311`, :issue:`22315`, :issue:`26974`)
814-
- Deprecated the :meth:`astype` method of datetimelike (``timedelta64[ns]``, ``datetime64[ns]``, ``Datetime64TZDtype``, ``PeriodDtype``) to convert to integer dtypes, use ``values.view(...)`` instead (:issue:`38544`)
814+
- Deprecated the :meth:`astype` method of datetimelike (``timedelta64[ns]``, ``datetime64[ns]``, ``Datetime64TZDtype``, ``PeriodDtype``) to convert to integer dtypes, use ``values.view(...)`` instead (:issue:`38544`). This deprecation was later reverted in pandas 1.4.0.
815815
- Deprecated :meth:`MultiIndex.is_lexsorted` and :meth:`MultiIndex.lexsort_depth`, use :meth:`MultiIndex.is_monotonic_increasing` instead (:issue:`32259`)
816816
- Deprecated keyword ``try_cast`` in :meth:`Series.where`, :meth:`Series.mask`, :meth:`DataFrame.where`, :meth:`DataFrame.mask`; cast results manually if desired (:issue:`38836`)
817817
- Deprecated comparison of :class:`Timestamp` objects with ``datetime.date`` objects. Instead of e.g. ``ts <= mydate`` use ``ts <= pd.Timestamp(mydate)`` or ``ts.date() <= mydate`` (:issue:`36131`)

pandas/core/arrays/datetimelike.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -430,14 +430,6 @@ def astype(self, dtype, copy: bool = True):
430430
elif is_integer_dtype(dtype):
431431
# we deliberately ignore int32 vs. int64 here.
432432
# See https://github.com/pandas-dev/pandas/issues/24381 for more.
433-
warnings.warn(
434-
f"casting {self.dtype} values to int64 with .astype(...) is "
435-
"deprecated and will raise in a future version. "
436-
"Use .view(...) instead.",
437-
FutureWarning,
438-
stacklevel=find_stack_level(),
439-
)
440-
441433
values = self.asi8
442434

443435
if is_unsigned_integer_dtype(dtype):

pandas/core/dtypes/astype.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,6 @@ def astype_nansafe(
112112

113113
elif is_datetime64_dtype(arr.dtype):
114114
if dtype == np.int64:
115-
warnings.warn(
116-
f"casting {arr.dtype} values to int64 with .astype(...) "
117-
"is deprecated and will raise in a future version. "
118-
"Use .view(...) instead.",
119-
FutureWarning,
120-
stacklevel=find_stack_level(),
121-
)
122115
if isna(arr).any():
123116
raise ValueError("Cannot convert NaT values to integer")
124117
return arr.view(dtype)
@@ -131,13 +124,6 @@ def astype_nansafe(
131124

132125
elif is_timedelta64_dtype(arr.dtype):
133126
if dtype == np.int64:
134-
warnings.warn(
135-
f"casting {arr.dtype} values to int64 with .astype(...) "
136-
"is deprecated and will raise in a future version. "
137-
"Use .view(...) instead.",
138-
FutureWarning,
139-
stacklevel=find_stack_level(),
140-
)
141127
if isna(arr).any():
142128
raise ValueError("Cannot convert NaT values to integer")
143129
return arr.view(dtype)

pandas/tests/arrays/period/test_astype.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,28 @@ def test_astype(dtype):
1313
# We choose to ignore the sign and size of integers for
1414
# Period/Datetime/Timedelta astype
1515
arr = period_array(["2000", "2001", None], freq="D")
16-
with tm.assert_produces_warning(FutureWarning):
17-
# astype(int..) deprecated
18-
result = arr.astype(dtype)
16+
result = arr.astype(dtype)
1917

2018
if np.dtype(dtype).kind == "u":
2119
expected_dtype = np.dtype("uint64")
2220
else:
2321
expected_dtype = np.dtype("int64")
2422

25-
with tm.assert_produces_warning(FutureWarning):
26-
# astype(int..) deprecated
27-
expected = arr.astype(expected_dtype)
23+
expected = arr.astype(expected_dtype)
2824

2925
assert result.dtype == expected_dtype
3026
tm.assert_numpy_array_equal(result, expected)
3127

3228

3329
def test_astype_copies():
3430
arr = period_array(["2000", "2001", None], freq="D")
35-
with tm.assert_produces_warning(FutureWarning):
36-
# astype(int..) deprecated
37-
result = arr.astype(np.int64, copy=False)
31+
result = arr.astype(np.int64, copy=False)
3832

3933
# Add the `.base`, since we now use `.asi8` which returns a view.
4034
# We could maybe override it in PeriodArray to return ._data directly.
4135
assert result.base is arr._data
4236

43-
with tm.assert_produces_warning(FutureWarning):
44-
# astype(int..) deprecated
45-
result = arr.astype(np.int64, copy=True)
37+
result = arr.astype(np.int64, copy=True)
4638
assert result is not arr._data
4739
tm.assert_numpy_array_equal(result, arr._data.view("i8"))
4840

pandas/tests/arrays/test_datetimes.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,13 @@ def test_astype_copies(self, dtype, other):
7777
@pytest.mark.parametrize("dtype", [int, np.int32, np.int64, "uint32", "uint64"])
7878
def test_astype_int(self, dtype):
7979
arr = DatetimeArray._from_sequence([pd.Timestamp("2000"), pd.Timestamp("2001")])
80-
with tm.assert_produces_warning(FutureWarning):
81-
# astype(int..) deprecated
82-
result = arr.astype(dtype)
80+
result = arr.astype(dtype)
8381

8482
if np.dtype(dtype).kind == "u":
8583
expected_dtype = np.dtype("uint64")
8684
else:
8785
expected_dtype = np.dtype("int64")
88-
89-
with tm.assert_produces_warning(FutureWarning):
90-
# astype(int..) deprecated
91-
expected = arr.astype(expected_dtype)
86+
expected = arr.astype(expected_dtype)
9287

9388
assert result.dtype == expected_dtype
9489
tm.assert_numpy_array_equal(result, expected)

pandas/tests/arrays/test_timedeltas.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,13 @@ class TestTimedeltaArray:
1111
@pytest.mark.parametrize("dtype", [int, np.int32, np.int64, "uint32", "uint64"])
1212
def test_astype_int(self, dtype):
1313
arr = TimedeltaArray._from_sequence([Timedelta("1H"), Timedelta("2H")])
14-
with tm.assert_produces_warning(FutureWarning):
15-
# astype(int..) deprecated
16-
result = arr.astype(dtype)
14+
result = arr.astype(dtype)
1715

1816
if np.dtype(dtype).kind == "u":
1917
expected_dtype = np.dtype("uint64")
2018
else:
2119
expected_dtype = np.dtype("int64")
22-
23-
with tm.assert_produces_warning(FutureWarning):
24-
# astype(int..) deprecated
25-
expected = arr.astype(expected_dtype)
20+
expected = arr.astype(expected_dtype)
2621

2722
assert result.dtype == expected_dtype
2823
tm.assert_numpy_array_equal(result, expected)

pandas/tests/dtypes/test_common.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -743,9 +743,7 @@ def test_astype_nansafe(val, typ):
743743

744744
msg = "Cannot convert NaT values to integer"
745745
with pytest.raises(ValueError, match=msg):
746-
with tm.assert_produces_warning(FutureWarning):
747-
# datetimelike astype(int64) deprecated
748-
astype_nansafe(arr, dtype=typ)
746+
astype_nansafe(arr, dtype=typ)
749747

750748

751749
def test_astype_nansafe_copy_false(any_int_numpy_dtype):

pandas/tests/indexes/datetimes/methods/test_astype.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ def test_astype(self):
3232
)
3333
tm.assert_index_equal(result, expected)
3434

35-
with tm.assert_produces_warning(FutureWarning):
36-
result = idx.astype(int)
35+
result = idx.astype(int)
3736
expected = Int64Index(
3837
[1463356800000000000] + [-9223372036854775808] * 3,
3938
dtype=np.int64,
@@ -42,8 +41,7 @@ def test_astype(self):
4241
tm.assert_index_equal(result, expected)
4342

4443
rng = date_range("1/1/2000", periods=10, name="idx")
45-
with tm.assert_produces_warning(FutureWarning):
46-
result = rng.astype("i8")
44+
result = rng.astype("i8")
4745
tm.assert_index_equal(result, Index(rng.asi8, name="idx"))
4846
tm.assert_numpy_array_equal(result.values, rng.asi8)
4947

@@ -53,9 +51,8 @@ def test_astype_uint(self):
5351
np.array([946684800000000000, 946771200000000000], dtype="uint64"),
5452
name="idx",
5553
)
56-
with tm.assert_produces_warning(FutureWarning):
57-
tm.assert_index_equal(arr.astype("uint64"), expected)
58-
tm.assert_index_equal(arr.astype("uint32"), expected)
54+
tm.assert_index_equal(arr.astype("uint64"), expected)
55+
tm.assert_index_equal(arr.astype("uint32"), expected)
5956

6057
def test_astype_with_tz(self):
6158

pandas/tests/indexes/interval/test_astype.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,10 @@ def index(self, request):
205205
@pytest.mark.parametrize("subtype", ["int64", "uint64"])
206206
def test_subtype_integer(self, index, subtype):
207207
dtype = IntervalDtype(subtype, "right")
208-
with tm.assert_produces_warning(FutureWarning):
209-
result = index.astype(dtype)
210-
expected = IntervalIndex.from_arrays(
211-
index.left.astype(subtype),
212-
index.right.astype(subtype),
213-
closed=index.closed,
214-
)
208+
result = index.astype(dtype)
209+
expected = IntervalIndex.from_arrays(
210+
index.left.astype(subtype), index.right.astype(subtype), closed=index.closed
211+
)
215212
tm.assert_index_equal(result, expected)
216213

217214
def test_subtype_float(self, index):

pandas/tests/indexes/interval/test_constructors.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,13 @@ def test_constructor(self, constructor, breaks, closed, name):
7474
)
7575
def test_constructor_dtype(self, constructor, breaks, subtype):
7676
# GH 19262: conversion via dtype parameter
77-
warn = None
78-
if subtype == "int64" and breaks.dtype.kind in ["M", "m"]:
79-
# astype(int64) deprecated
80-
warn = FutureWarning
81-
82-
with tm.assert_produces_warning(warn):
83-
expected_kwargs = self.get_kwargs_from_breaks(breaks.astype(subtype))
77+
expected_kwargs = self.get_kwargs_from_breaks(breaks.astype(subtype))
8478
expected = constructor(**expected_kwargs)
8579

8680
result_kwargs = self.get_kwargs_from_breaks(breaks)
8781
iv_dtype = IntervalDtype(subtype, "right")
8882
for dtype in (iv_dtype, str(iv_dtype)):
89-
with tm.assert_produces_warning(warn):
90-
91-
result = constructor(dtype=dtype, **result_kwargs)
83+
result = constructor(dtype=dtype, **result_kwargs)
9284
tm.assert_index_equal(result, expected)
9385

9486
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)