Skip to content

DEPR: object inference in to_stata #56536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
-
Expand Down
8 changes: 8 additions & 0 deletions pandas/io/stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
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.",
FutureWarning,
stacklevel=find_stack_level(),
)
if delta:
delta = dates._values - stata_epoch

Expand Down
7 changes: 6 additions & 1 deletion pandas/tests/io/test_stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,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(
Expand Down
Loading