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

+1-1
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

-8
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

-14
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

+4-12
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

+2-7
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

+2-7
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

+1-3
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

+4-7
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

+4-7
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

+2-10
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(

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

+4-7
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ def test_astype_conversion(self):
3939
)
4040
tm.assert_index_equal(result, expected)
4141

42-
with tm.assert_produces_warning(FutureWarning):
43-
result = idx.astype(np.int64)
42+
result = idx.astype(np.int64)
4443
expected = Int64Index(
4544
[16937] + [-9223372036854775808] * 3, dtype=np.int64, name="idx"
4645
)
@@ -51,17 +50,15 @@ def test_astype_conversion(self):
5150
tm.assert_index_equal(result, expected)
5251

5352
idx = period_range("1990", "2009", freq="A", name="idx")
54-
with tm.assert_produces_warning(FutureWarning):
55-
result = idx.astype("i8")
53+
result = idx.astype("i8")
5654
tm.assert_index_equal(result, Index(idx.asi8, name="idx"))
5755
tm.assert_numpy_array_equal(result.values, idx.asi8)
5856

5957
def test_astype_uint(self):
6058
arr = period_range("2000", periods=2, name="idx")
6159
expected = UInt64Index(np.array([10957, 10958], dtype="uint64"), name="idx")
62-
with tm.assert_produces_warning(FutureWarning):
63-
tm.assert_index_equal(arr.astype("uint64"), expected)
64-
tm.assert_index_equal(arr.astype("uint32"), expected)
60+
tm.assert_index_equal(arr.astype("uint64"), expected)
61+
tm.assert_index_equal(arr.astype("uint32"), expected)
6562

6663
def test_astype_object(self):
6764
idx = PeriodIndex([], freq="M")

pandas/tests/indexes/test_common.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010

1111
from pandas.compat import IS64
1212

13-
from pandas.core.dtypes.common import (
14-
is_integer_dtype,
15-
needs_i8_conversion,
16-
)
13+
from pandas.core.dtypes.common import is_integer_dtype
1714

1815
import pandas as pd
1916
from pandas import (
@@ -383,10 +380,7 @@ def test_astype_preserves_name(self, index, dtype):
383380
index.name = "idx"
384381

385382
warn = None
386-
if dtype in ["int64", "uint64"]:
387-
if needs_i8_conversion(index.dtype):
388-
warn = FutureWarning
389-
elif (
383+
if (
390384
isinstance(index, DatetimeIndex)
391385
and index.tz is not None
392386
and dtype == "datetime64[ns]"

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

+4-7
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ def test_astype(self):
5858
)
5959
tm.assert_index_equal(result, expected)
6060

61-
with tm.assert_produces_warning(FutureWarning):
62-
result = idx.astype(int)
61+
result = idx.astype(int)
6362
expected = Int64Index(
6463
[100000000000000] + [-9223372036854775808] * 3, dtype=np.int64, name="idx"
6564
)
@@ -70,8 +69,7 @@ def test_astype(self):
7069
tm.assert_index_equal(result, expected)
7170

7271
rng = timedelta_range("1 days", periods=10)
73-
with tm.assert_produces_warning(FutureWarning):
74-
result = rng.astype("i8")
72+
result = rng.astype("i8")
7573
tm.assert_index_equal(result, Index(rng.asi8))
7674
tm.assert_numpy_array_equal(rng.asi8, result.values)
7775

@@ -80,9 +78,8 @@ def test_astype_uint(self):
8078
expected = UInt64Index(
8179
np.array([3600000000000, 90000000000000], dtype="uint64")
8280
)
83-
with tm.assert_produces_warning(FutureWarning):
84-
tm.assert_index_equal(arr.astype("uint64"), expected)
85-
tm.assert_index_equal(arr.astype("uint32"), expected)
81+
tm.assert_index_equal(arr.astype("uint64"), expected)
82+
tm.assert_index_equal(arr.astype("uint32"), expected)
8683

8784
def test_astype_timedelta64(self):
8885
# GH 13149, GH 13209

pandas/tests/internals/test_internals.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -540,9 +540,6 @@ def test_astype(self, t):
540540
# coerce all
541541
mgr = create_mgr("c: f4; d: f2; e: f8")
542542

543-
warn = FutureWarning if t == "int64" else None
544-
# datetimelike.astype(int64) deprecated
545-
546543
t = np.dtype(t)
547544
tmgr = mgr.astype(t)
548545
assert tmgr.iget(0).dtype.type == t
@@ -553,8 +550,7 @@ def test_astype(self, t):
553550
mgr = create_mgr("a,b: object; c: bool; d: datetime; e: f4; f: f2; g: f8")
554551

555552
t = np.dtype(t)
556-
with tm.assert_produces_warning(warn):
557-
tmgr = mgr.astype(t, errors="ignore")
553+
tmgr = mgr.astype(t, errors="ignore")
558554
assert tmgr.iget(2).dtype.type == t
559555
assert tmgr.iget(4).dtype.type == t
560556
assert tmgr.iget(5).dtype.type == t

pandas/tests/series/test_constructors.py

+6-14
Original file line numberDiff line numberDiff line change
@@ -968,9 +968,7 @@ def test_constructor_dtype_datetime64_10(self):
968968
dts = Series(dates, dtype="datetime64[ns]")
969969

970970
# valid astype
971-
with tm.assert_produces_warning(FutureWarning):
972-
# astype(np.int64) deprecated
973-
dts.astype("int64")
971+
dts.astype("int64")
974972

975973
# invalid casting
976974
msg = r"cannot astype a datetimelike from \[datetime64\[ns\]\] to \[int32\]"
@@ -980,10 +978,8 @@ def test_constructor_dtype_datetime64_10(self):
980978
# ints are ok
981979
# we test with np.int64 to get similar results on
982980
# windows / 32-bit platforms
983-
with tm.assert_produces_warning(FutureWarning):
984-
# astype(np.int64) deprecated
985-
result = Series(dts, dtype=np.int64)
986-
expected = Series(dts.astype(np.int64))
981+
result = Series(dts, dtype=np.int64)
982+
expected = Series(dts.astype(np.int64))
987983
tm.assert_series_equal(result, expected)
988984

989985
def test_constructor_dtype_datetime64_9(self):
@@ -1494,9 +1490,7 @@ def test_constructor_dtype_timedelta64(self):
14941490
assert td.dtype == "timedelta64[ns]"
14951491

14961492
# valid astype
1497-
with tm.assert_produces_warning(FutureWarning):
1498-
# astype(int64) deprecated
1499-
td.astype("int64")
1493+
td.astype("int64")
15001494

15011495
# invalid casting
15021496
msg = r"cannot astype a datetimelike from \[timedelta64\[ns\]\] to \[int32\]"
@@ -1622,10 +1616,8 @@ def test_constructor_cant_cast_datetimelike(self, index):
16221616
# ints are ok
16231617
# we test with np.int64 to get similar results on
16241618
# windows / 32-bit platforms
1625-
with tm.assert_produces_warning(FutureWarning):
1626-
# asype(np.int64) deprecated, use .view(np.int64) instead
1627-
result = Series(index, dtype=np.int64)
1628-
expected = Series(index.astype(np.int64))
1619+
result = Series(index, dtype=np.int64)
1620+
expected = Series(index.astype(np.int64))
16291621
tm.assert_series_equal(result, expected)
16301622

16311623
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)