diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 38be038efcaa5..efa22c99a0aa5 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -228,7 +228,7 @@ class DatetimeArray(dtl.TimelikeOps, dtl.DatelikeOps): _is_recognized_dtype: Callable[[DtypeObj], bool] = lambda x: lib.is_np_dtype( x, "M" ) or isinstance(x, DatetimeTZDtype) - _infer_matches = ("datetime", "datetime64", "date") + _infer_matches = ("datetime", "datetime64") @property def _scalar_type(self) -> type[Timestamp]: diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index ddde24e72c65c..86ddd05a9e85a 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -39,7 +39,6 @@ no_default, ) from pandas._libs.tslibs import ( - OutOfBoundsDatetime, Timestamp, tz_compare, ) @@ -6216,11 +6215,6 @@ def _maybe_downcast_for_indexing(self, other: Index) -> tuple[Index, Index]: # standardize on UTC return self.tz_convert("UTC"), other.tz_convert("UTC") - elif self.inferred_type == "date" and isinstance(other, ABCDatetimeIndex): - try: - return type(other)(self), other - except OutOfBoundsDatetime: - return self, other elif self.inferred_type == "timedelta" and isinstance(other, ABCTimedeltaIndex): # TODO: we dont have tests that get here return type(other)(self), other diff --git a/pandas/tests/frame/methods/test_asfreq.py b/pandas/tests/frame/methods/test_asfreq.py index 1c3c41e2e0299..5975f5501e6f6 100644 --- a/pandas/tests/frame/methods/test_asfreq.py +++ b/pandas/tests/frame/methods/test_asfreq.py @@ -186,15 +186,15 @@ def test_asfreq_fillvalue(self): tm.assert_series_equal(expected_series, actual_series) def test_asfreq_with_date_object_index(self, frame_or_series): + # GH#62158 date objects lose indexing special case rng = date_range("1/1/2000", periods=20) ts = frame_or_series(np.random.default_rng(2).standard_normal(20), index=rng) + ts.index = [x.date() for x in ts.index] - ts2 = ts.copy() - ts2.index = [x.date() for x in ts2.index] - - result = ts2.asfreq("4h", method="ffill") - expected = ts.asfreq("4h", method="ffill") - tm.assert_equal(result, expected) + with pytest.raises( + TypeError, match="Cannot compare Timestamp with datetime.date" + ): + ts.asfreq("4h", method="ffill") def test_asfreq_with_unsorted_index(self, frame_or_series): # GH#39805 diff --git a/pandas/tests/indexes/datetimes/test_indexing.py b/pandas/tests/indexes/datetimes/test_indexing.py index 5877a38bbee11..51468550f030d 100644 --- a/pandas/tests/indexes/datetimes/test_indexing.py +++ b/pandas/tests/indexes/datetimes/test_indexing.py @@ -535,10 +535,11 @@ def test_get_indexer_pyarrow(self, as_td): tm.assert_numpy_array_equal(result2, expected) def test_get_indexer_date_objs(self): + # GH#62158 date objects lose indexing special case rng = date_range("1/1/2000", periods=20) - result = rng.get_indexer(rng.map(lambda x: x.date())) - expected = rng.get_indexer(rng) + + expected = np.full(len(rng), -1, dtype=np.intp) tm.assert_numpy_array_equal(result, expected) def test_get_indexer(self): @@ -583,17 +584,23 @@ def test_get_indexer(self): idx.get_indexer(idx[[0]], method="nearest", tolerance="foo") @pytest.mark.parametrize( - "target", + "target, expected", [ - [date(2020, 1, 1), Timestamp("2020-01-02")], - [Timestamp("2020-01-01"), date(2020, 1, 2)], + ( + [date(2020, 1, 1), Timestamp("2020-01-02")], + np.array([-1, 1], dtype=np.intp), + ), + ( + [Timestamp("2020-01-01"), date(2020, 1, 2)], + np.array([0, -1], dtype=np.intp), + ), ], ) - def test_get_indexer_mixed_dtypes(self, target): - # https://github.com/pandas-dev/pandas/issues/33741 + def test_get_indexer_mixed_dtypes(self, target, expected): + # GH#33741 regression test: mixed dtypes should not error + # GH#62158 date objects lose indexing special case values = DatetimeIndex([Timestamp("2020-01-01"), Timestamp("2020-01-02")]) result = values.get_indexer(target) - expected = np.array([0, 1], dtype=np.intp) tm.assert_numpy_array_equal(result, expected) @pytest.mark.parametrize( diff --git a/pandas/tests/io/parser/dtypes/test_categorical.py b/pandas/tests/io/parser/dtypes/test_categorical.py index c157510e552b4..910edcd837878 100644 --- a/pandas/tests/io/parser/dtypes/test_categorical.py +++ b/pandas/tests/io/parser/dtypes/test_categorical.py @@ -275,13 +275,24 @@ def test_categorical_coerces_numeric(all_parsers): def test_categorical_coerces_datetime(all_parsers): parser = all_parsers dti = pd.DatetimeIndex(["2017-01-01", "2018-01-01", "2019-01-01"], freq=None) + dtype = {"b": CategoricalDtype(dti)} data = "b\n2017-01-01\n2018-01-01\n2019-01-01" expected = DataFrame({"b": Categorical(dtype["b"].categories)}) - result = parser.read_csv(StringIO(data), dtype=dtype) - tm.assert_frame_equal(result, expected) + if parser.engine == "pyarrow": + msg = "Constructing a Categorical with a dtype and values containing" + with tm.assert_produces_warning( + Pandas4Warning, match=msg, check_stacklevel=False + ): + result = parser.read_csv(StringIO(data), dtype=dtype) + tm.assert_series_equal( + result["b"].isna(), pd.Series([True, True, True], name="b") + ) + else: + result = parser.read_csv(StringIO(data), dtype=dtype) + tm.assert_frame_equal(result, expected) def test_categorical_coerces_timestamp(all_parsers): @@ -291,8 +302,16 @@ def test_categorical_coerces_timestamp(all_parsers): data = "b\n2014-01-01\n2014-01-01" expected = DataFrame({"b": Categorical([Timestamp("2014")] * 2)}) - result = parser.read_csv(StringIO(data), dtype=dtype) - tm.assert_frame_equal(result, expected) + if parser.engine == "pyarrow": + msg = "Constructing a Categorical with a dtype and values containing" + with tm.assert_produces_warning( + Pandas4Warning, match=msg, check_stacklevel=False + ): + result = parser.read_csv(StringIO(data), dtype=dtype) + tm.assert_series_equal(result["b"].isna(), pd.Series([True, True], name="b")) + else: + result = parser.read_csv(StringIO(data), dtype=dtype) + tm.assert_frame_equal(result, expected) def test_categorical_coerces_timedelta(all_parsers): diff --git a/pandas/tests/series/test_arithmetic.py b/pandas/tests/series/test_arithmetic.py index 35a9742d653db..2f3a04af88189 100644 --- a/pandas/tests/series/test_arithmetic.py +++ b/pandas/tests/series/test_arithmetic.py @@ -758,6 +758,7 @@ def test_datetime_understood(self, unit): tm.assert_series_equal(result, expected) def test_align_date_objects_with_datetimeindex(self): + # GH#62158: datetime.date objects no longer auto-align with Timestamps rng = date_range("1/1/2000", periods=20) ts = Series(np.random.default_rng(2).standard_normal(20), index=rng) @@ -767,10 +768,11 @@ def test_align_date_objects_with_datetimeindex(self): result = ts + ts2 result2 = ts2 + ts - expected = ts + ts[5:] - expected.index = expected.index._with_freq(None) + expected = Series(dtype=float, index=result.index) + expected2 = Series(dtype=float, index=result2.index) + tm.assert_series_equal(result, expected) - tm.assert_series_equal(result2, expected) + tm.assert_series_equal(result2, expected2) class TestNamePreservation: