Skip to content

Commit 453640d

Browse files
committed
DEPR: object inference in to_stata
1 parent 4d67bb2 commit 453640d

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

pandas/io/stata.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,14 @@ 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+
# - Deprecated casting object-dtype columns of datetimes to datetime64 when writing to stata; call df=df.infer_objects() before writing to stata instead (:issue:`??`)
397+
warnings.warn(
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

pandas/tests/io/test_stata.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,12 @@ 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+
with tm.assert_produces_warning(FutureWarning, match=msg):
1038+
expected.to_stata(path, convert_dates=date_conversion)
10341039
written_and_read_again = self.read_dta(path)
10351040

10361041
tm.assert_frame_equal(

0 commit comments

Comments
 (0)