From 453640dca482895edc3ff700dfac878424148883 Mon Sep 17 00:00:00 2001 From: Brock Date: Sun, 17 Dec 2023 10:54:34 -0800 Subject: [PATCH 1/4] 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 092c24f0d31c3..1552f174ce70f 100644 --- a/pandas/io/stata.py +++ b/pandas/io/stata.py @@ -393,6 +393,14 @@ def parse_dates_safe( d["days"] = np.asarray(diff).astype("m8[D]").view("int64") 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 b155c0cca4aa6..d6a3aaa6c5f05 100644 --- a/pandas/tests/io/test_stata.py +++ b/pandas/tests/io/test_stata.py @@ -1030,7 +1030,12 @@ def test_big_dates(self, datapath, temp_file): # {c : c[-2:] for c in columns} path = temp_file 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 58277310bd496e9e9012f6c2cf2b7d69fb190a9c Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 31 May 2024 14:21:54 -0700 Subject: [PATCH 2/4] 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 5ff1ea9d194f6..cd6c8c67bb3ed 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -456,6 +456,7 @@ Other Deprecations - 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 lowercase strings ``d``, ``b`` and ``c`` denoting frequencies in :class:`Day`, :class:`BusinessDay` and :class:`CustomBusinessDay` in favour of ``D``, ``B`` and ``C`` (:issue:`58998`) - Deprecated lowercase strings ``w``, ``w-mon``, ``w-tue``, etc. denoting frequencies in :class:`Week` in favour of ``W``, ``W-MON``, ``W-TUE``, etc. (:issue:`58998`) +- 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 strings ``w``, ``d``, ``MIN``, ``MS``, ``US`` and ``NS`` denoting units in :class:`Timedelta` in favour of ``W``, ``D``, ``min``, ``ms``, ``us`` and ``ns`` (:issue:`59051`) - Deprecated the ``arg`` parameter of ``Series.map``; pass the added ``func`` argument instead. (:issue:`61260`) From bc0014a6b32c347a46355c76f1d6a6e719d3bdaa Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 2 Jul 2025 09:48:57 -0700 Subject: [PATCH 3/4] Fix broken test --- pandas/io/stata.py | 6 +++--- pandas/tests/io/test_stata.py | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/pandas/io/stata.py b/pandas/io/stata.py index 1552f174ce70f..08177e76ee237 100644 --- a/pandas/io/stata.py +++ b/pandas/io/stata.py @@ -393,8 +393,8 @@ def parse_dates_safe( d["days"] = np.asarray(diff).astype("m8[D]").view("int64") 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( + # GH#56536 "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.", @@ -405,10 +405,10 @@ def parse_dates_safe( delta = dates._values - stata_epoch def f(x: timedelta) -> float: - return US_PER_DAY * x.days + 1000000 * x.seconds + x.microseconds + return US_PER_DAY * x.days + 1_000_000 * x.seconds + x.microseconds v = np.vectorize(f) - d["delta"] = v(delta) + d["delta"] = v(delta) // 1_000 # convert back to ms if year: year_month = dates.apply(lambda x: 100 * x.year + x.month) d["year"] = year_month._values // 100 diff --git a/pandas/tests/io/test_stata.py b/pandas/tests/io/test_stata.py index d6a3aaa6c5f05..90fda2c10962b 100644 --- a/pandas/tests/io/test_stata.py +++ b/pandas/tests/io/test_stata.py @@ -1034,8 +1034,9 @@ def test_big_dates(self, datapath, temp_file): "Converting object-dtype columns of datetimes to datetime64 " "when writing to stata is deprecated" ) + exp_object = expected.astype(object) with tm.assert_produces_warning(FutureWarning, match=msg): - expected.to_stata(path, convert_dates=date_conversion) + exp_object.to_stata(path, convert_dates=date_conversion) written_and_read_again = self.read_dta(path) tm.assert_frame_equal( From 624f3424a8d2a5b227a289c21c443490e9b809fa Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 2 Jul 2025 11:16:45 -0700 Subject: [PATCH 4/4] alphabetize --- doc/source/whatsnew/v3.0.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index cd6c8c67bb3ed..d3914ac451c6e 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -454,9 +454,9 @@ Other Deprecations - Deprecated allowing non-keyword arguments in :meth:`Series.to_string` except ``buf``. (:issue:`57280`) - Deprecated behavior of :meth:`.DataFrameGroupBy.groups` and :meth:`.SeriesGroupBy.groups`, in a future version ``groups`` by one element list will return tuple instead of scalar. (:issue:`58858`) - 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 lowercase strings ``d``, ``b`` and ``c`` denoting frequencies in :class:`Day`, :class:`BusinessDay` and :class:`CustomBusinessDay` in favour of ``D``, ``B`` and ``C`` (:issue:`58998`) - Deprecated lowercase strings ``w``, ``w-mon``, ``w-tue``, etc. denoting frequencies in :class:`Week` in favour of ``W``, ``W-MON``, ``W-TUE``, etc. (:issue:`58998`) -- 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 strings ``w``, ``d``, ``MIN``, ``MS``, ``US`` and ``NS`` denoting units in :class:`Timedelta` in favour of ``W``, ``D``, ``min``, ``ms``, ``us`` and ``ns`` (:issue:`59051`) - Deprecated the ``arg`` parameter of ``Series.map``; pass the added ``func`` argument instead. (:issue:`61260`)