From b379d543cc4013fe01e5e977132bd09966419e74 Mon Sep 17 00:00:00 2001 From: Brock Date: Sun, 17 Dec 2023 10:54:34 -0800 Subject: [PATCH 1/2] DEPR: object inference in to_stata --- pandas/io/stata.py | 8 ++++++++ pandas/tests/io/test_stata.py | 7 ++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/pandas/io/stata.py b/pandas/io/stata.py index 52c664a5c8a37..e8405a5876b74 100644 --- a/pandas/io/stata.py +++ b/pandas/io/stata.py @@ -429,6 +429,14 @@ def parse_dates_safe( d["days"] = days_in_ns // NS_PER_DAY elif infer_dtype(dates, skipna=False) == "datetime": + # - Deprecated casting object-dtype columns of datetimes to datetime64 when writing to stata; call df=df.infer_objects() before writing to stata instead (:issue:`??`) + warnings.warn( + "Converting object-dtype columns of datetimes to datetime64 when " + "writing to stata is deprecated. Call " + "`df=df.infer_objects(copy=False)` before writing to stata instead.", + FutureWarning, + stacklevel=find_stack_level(), + ) if delta: delta = dates._values - stata_epoch diff --git a/pandas/tests/io/test_stata.py b/pandas/tests/io/test_stata.py index 3e4e1a107da9d..513da96454325 100644 --- a/pandas/tests/io/test_stata.py +++ b/pandas/tests/io/test_stata.py @@ -885,7 +885,12 @@ def test_big_dates(self, datapath): # {c : c[-2:] for c in columns} with tm.ensure_clean() as path: expected.index.name = "index" - expected.to_stata(path, convert_dates=date_conversion) + msg = ( + "Converting object-dtype columns of datetimes to datetime64 " + "when writing to stata is deprecated" + ) + with tm.assert_produces_warning(FutureWarning, match=msg): + expected.to_stata(path, convert_dates=date_conversion) written_and_read_again = self.read_dta(path) tm.assert_frame_equal( From 173ded214afb2db94696125d8e8883470726dd73 Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 31 May 2024 14:21:54 -0700 Subject: [PATCH 2/2] Whatsnew --- doc/source/whatsnew/v3.0.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 865996bdf8892..a24c32ff213b9 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -269,6 +269,7 @@ Other Deprecations - Deprecated allowing non-keyword arguments in :meth:`Series.to_markdown` except ``buf``. (:issue:`57280`) - Deprecated allowing non-keyword arguments in :meth:`Series.to_string` except ``buf``. (:issue:`57280`) - Deprecated behavior of :meth:`Series.dt.to_pytimedelta`, in a future version this will return a :class:`Series` containing python ``datetime.timedelta`` objects instead of an ``ndarray`` of timedelta; this matches the behavior of other :meth:`Series.dt` properties. (:issue:`57463`) +- Deprecated converting object-dtype columns of ``datetime.datetime`` objects to datetime64 when writing to stata (:issue:`56536`) - Deprecated parameter ``method`` in :meth:`DataFrame.reindex_like` / :meth:`Series.reindex_like` (:issue:`58667`) - Deprecated using ``epoch`` date format in :meth:`DataFrame.to_json` and :meth:`Series.to_json`, use ``iso`` instead. (:issue:`57063`) -