Skip to content

Commit b91fa1d

Browse files
authored
DEPR: object inference in to_stata (#56536)
* DEPR: object inference in to_stata * Whatsnew * Fix broken test * alphabetize
1 parent 22f12fc commit b91fa1d

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ Other Deprecations
456456
- Deprecated allowing non-keyword arguments in :meth:`Series.to_string` except ``buf``. (:issue:`57280`)
457457
- 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`)
458458
- 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`)
459+
- Deprecated converting object-dtype columns of ``datetime.datetime`` objects to datetime64 when writing to stata (:issue:`56536`)
459460
- 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`)
460461
- 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`)
461462
- Deprecated parameter ``method`` in :meth:`DataFrame.reindex_like` / :meth:`Series.reindex_like` (:issue:`58667`)

pandas/io/stata.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,14 +393,22 @@ def parse_dates_safe(
393393
d["days"] = np.asarray(diff).astype("m8[D]").view("int64")
394394

395395
elif infer_dtype(dates, skipna=False) == "datetime":
396+
warnings.warn(
397+
# GH#56536
398+
"Converting object-dtype columns of datetimes to datetime64 when "
399+
"writing to stata is deprecated. Call "
400+
"`df=df.infer_objects(copy=False)` before writing to stata instead.",
401+
FutureWarning,
402+
stacklevel=find_stack_level(),
403+
)
396404
if delta:
397405
delta = dates._values - stata_epoch
398406

399407
def f(x: timedelta) -> float:
400-
return US_PER_DAY * x.days + 1000000 * x.seconds + x.microseconds
408+
return US_PER_DAY * x.days + 1_000_000 * x.seconds + x.microseconds
401409

402410
v = np.vectorize(f)
403-
d["delta"] = v(delta)
411+
d["delta"] = v(delta) // 1_000 # convert back to ms
404412
if year:
405413
year_month = dates.apply(lambda x: 100 * x.year + x.month)
406414
d["year"] = year_month._values // 100

pandas/tests/io/test_stata.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,13 @@ def test_big_dates(self, datapath, temp_file):
10301030
# {c : c[-2:] for c in columns}
10311031
path = temp_file
10321032
expected.index.name = "index"
1033-
expected.to_stata(path, convert_dates=date_conversion)
1033+
msg = (
1034+
"Converting object-dtype columns of datetimes to datetime64 "
1035+
"when writing to stata is deprecated"
1036+
)
1037+
exp_object = expected.astype(object)
1038+
with tm.assert_produces_warning(FutureWarning, match=msg):
1039+
exp_object.to_stata(path, convert_dates=date_conversion)
10341040
written_and_read_again = self.read_dta(path)
10351041

10361042
tm.assert_frame_equal(

0 commit comments

Comments
 (0)