Skip to content

Commit b94ea78

Browse files
committed
deprecate inference with scalar date
1 parent c75b7c7 commit b94ea78

File tree

5 files changed

+41
-25
lines changed

5 files changed

+41
-25
lines changed

pandas/core/dtypes/cast.py

+17-20
Original file line numberDiff line numberDiff line change
@@ -832,28 +832,25 @@ def infer_dtype_from_scalar(val) -> tuple[DtypeObj, Any]:
832832
pa_dtype = pa.time64("us")
833833
dtype = ArrowDtype(pa_dtype)
834834

835-
elif isinstance(val, dt.time):
836-
if val.tzinfo is None:
837-
# pyarrow doesn't have a dtype for timetz.
838-
opt = get_option("future.infer_time")
839-
if opt is None:
840-
warnings.warn(
841-
"Pandas type inference with a `datetime.time` "
842-
"object is deprecated. In a future version, this will give "
843-
"time32[pyarrow] dtype, which will require pyarrow to be "
844-
"installed. To opt in to the new behavior immediately set "
845-
"`pd.set_option('future.infer_time', True)`. To keep the "
846-
"old behavior pass `dtype=object`.",
847-
FutureWarning,
848-
stacklevel=find_stack_level(),
849-
)
850-
elif opt is True:
851-
import pyarrow as pa
835+
elif isinstance(val, dt.date):
836+
opt = get_option("future.infer_date")
837+
if opt is None:
838+
warnings.warn(
839+
"Pandas type inference with a `datetime.date` "
840+
"object is deprecated. In a future version, this will give "
841+
"date32[pyarrow] dtype, which will require pyarrow to be "
842+
"installed. To opt in to the new behavior immediately set "
843+
"`pd.set_option('future.infer_date', True)`. To keep the "
844+
"old behavior pass `dtype=object`.",
845+
FutureWarning,
846+
stacklevel=find_stack_level(),
847+
)
848+
elif opt is True:
849+
import pyarrow as pa
852850

853-
pa_dtype = pa.time64("us")
854-
from pandas.core.arrays.arrow import ArrowDtype
851+
pa_dtype = pa.date32()
855852

856-
dtype = ArrowDtype(pa_dtype)
853+
dtype = ArrowDtype(pa_dtype)
857854

858855
elif is_bool(val):
859856
dtype = np.dtype(np.bool_)

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):
@@ -102,7 +103,9 @@ def test_infer_dtype_from_period(freq):
102103

103104
def test_infer_dtype_misc():
104105
dt = date(2000, 1, 1)
105-
dtype, val = infer_dtype_from_scalar(dt)
106+
msg = "type inference with a `datetime.date` object"
107+
with tm.assert_produces_warning(FutureWarning, match=msg):
108+
dtype, val = infer_dtype_from_scalar(dt)
106109
assert dtype == np.object_
107110

108111
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
@@ -1443,7 +1443,10 @@ def test_loc_setitem_datetime_coercion(self):
14431443
df.loc[0:1, "c"] = np.datetime64("2008-08-08")
14441444
assert Timestamp("2008-08-08") == df.loc[0, "c"]
14451445
assert Timestamp("2008-08-08") == df.loc[1, "c"]
1446-
df.loc[2, "c"] = date(2005, 5, 5)
1446+
1447+
warn_msg = "type inference with a `datetime.date` object"
1448+
with tm.assert_produces_warning(FutureWarning, match=warn_msg):
1449+
df.loc[2, "c"] = date(2005, 5, 5)
14471450
assert Timestamp("2005-05-05").date() == df.loc[2, "c"]
14481451

14491452
@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)