Skip to content

Commit 04147d1

Browse files
committed
deprecate inference with scalar date
1 parent d9cea17 commit 04147d1

File tree

5 files changed

+45
-5
lines changed

5 files changed

+45
-5
lines changed

pandas/core/dtypes/cast.py

+21
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,27 @@ def infer_dtype_from_scalar(val) -> tuple[DtypeObj, Any]:
821821

822822
dtype = ArrowDtype(pa_dtype)
823823

824+
elif isinstance(val, dt.date):
825+
opt = get_option("future.infer_date")
826+
if opt is None:
827+
warnings.warn(
828+
"Pandas type inference with a `datetime.date` "
829+
"object is deprecated. In a future version, this will give "
830+
"date32[pyarrow] dtype, which will require pyarrow to be "
831+
"installed. To opt in to the new behavior immediately set "
832+
"`pd.set_option('future.infer_date', True)`. To keep the "
833+
"old behavior pass `dtype=object`.",
834+
FutureWarning,
835+
stacklevel=find_stack_level(),
836+
)
837+
elif opt is True:
838+
import pyarrow as pa
839+
840+
pa_dtype = pa.date32()
841+
from pandas.core.arrays.arrow import ArrowDtype
842+
843+
dtype = ArrowDtype(pa_dtype)
844+
824845
elif is_bool(val):
825846
dtype = np.dtype(np.bool_)
826847

pandas/tests/dtypes/cast/test_infer_dtype.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
Timestamp,
2424
date_range,
2525
)
26+
import pandas._testing as tm
2627

2728

2829
def test_infer_dtype_from_int_scalar(any_int_numpy_dtype):
@@ -88,7 +89,9 @@ def test_infer_dtype_from_period(freq):
8889

8990
def test_infer_dtype_misc():
9091
dt = date(2000, 1, 1)
91-
dtype, val = infer_dtype_from_scalar(dt)
92+
msg = "type inference with a `datetime.date` object"
93+
with tm.assert_produces_warning(FutureWarning, match=msg):
94+
dtype, val = infer_dtype_from_scalar(dt)
9295
assert dtype == np.object_
9396

9497
ts = Timestamp(1, tz="US/Eastern")

pandas/tests/dtypes/cast/test_promote.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from pandas.core.dtypes.missing import isna
1717

1818
import pandas as pd
19+
import pandas._testing as tm
1920

2021

2122
def _check_promote(dtype, fill_value, expected_dtype, exp_val_for_scalar=None):
@@ -354,12 +355,21 @@ def test_maybe_promote_any_with_datetime64(any_numpy_dtype, fill_value):
354355
expected_dtype = np.dtype(object)
355356
exp_val_for_scalar = fill_value
356357

358+
msg = "type inference with a `datetime.date` object"
359+
warn = None
360+
if type(fill_value) is datetime.date and any_numpy_dtype in [
361+
"datetime64[ns]",
362+
"timedelta64[ns]",
363+
]:
364+
warn = FutureWarning
365+
357366
if type(fill_value) is datetime.date and dtype.kind == "M":
358367
# Casting date to dt64 is deprecated, in 2.0 enforced to cast to object
359368
expected_dtype = np.dtype(object)
360369
exp_val_for_scalar = fill_value
361370

362-
_check_promote(dtype, fill_value, expected_dtype, exp_val_for_scalar)
371+
with tm.assert_produces_warning(warn, match=msg):
372+
_check_promote(dtype, fill_value, expected_dtype, exp_val_for_scalar)
363373

364374

365375
@pytest.mark.parametrize(

pandas/tests/indexing/test_loc.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1439,7 +1439,10 @@ def test_loc_setitem_datetime_coercion(self):
14391439
df.loc[0:1, "c"] = np.datetime64("2008-08-08")
14401440
assert Timestamp("2008-08-08") == df.loc[0, "c"]
14411441
assert Timestamp("2008-08-08") == df.loc[1, "c"]
1442-
df.loc[2, "c"] = date(2005, 5, 5)
1442+
1443+
warn_msg = "type inference with a `datetime.date` object"
1444+
with tm.assert_produces_warning(FutureWarning, match=warn_msg):
1445+
df.loc[2, "c"] = date(2005, 5, 5)
14431446
assert Timestamp("2005-05-05").date() == df.loc[2, "c"]
14441447

14451448
@pytest.mark.parametrize("idxer", ["var", ["var"]])

pandas/tests/io/pytables/test_errors.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,15 @@ def test_table_index_incompatible_dtypes(setup_path):
5656

5757

5858
def test_unimplemented_dtypes_table_columns(setup_path):
59+
warn_msg = "type inference with a `datetime.date` object"
5960
with ensure_clean_store(setup_path) as store:
6061
dtypes = [("date", datetime.date(2001, 1, 2))]
6162

6263
# currently not supported dtypes ####
6364
for n, f in dtypes:
6465
df = tm.makeDataFrame()
65-
df[n] = f
66+
with tm.assert_produces_warning(FutureWarning, match=warn_msg):
67+
df[n] = f
6668
msg = re.escape(f"[{n}] is not implemented as a table column")
6769
with pytest.raises(TypeError, match=msg):
6870
store.append(f"df1_{n}", df)
@@ -71,7 +73,8 @@ def test_unimplemented_dtypes_table_columns(setup_path):
7173
df = tm.makeDataFrame()
7274
df["obj1"] = "foo"
7375
df["obj2"] = "bar"
74-
df["datetime1"] = datetime.date(2001, 1, 2)
76+
with tm.assert_produces_warning(FutureWarning, match=warn_msg):
77+
df["datetime1"] = datetime.date(2001, 1, 2)
7578
df = df._consolidate()
7679

7780
with ensure_clean_store(setup_path) as store:

0 commit comments

Comments
 (0)