Skip to content

Commit 2d66e59

Browse files
committed
Updated test_stata.py
1 parent 0ce2747 commit 2d66e59

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

pandas/tests/io/test_stata.py

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2593,24 +2593,32 @@ def test_many_strl(temp_file, version):
25932593
def test_convert_dates_key_handling(tmp_path, version):
25942594
temp_file = tmp_path / "test.dta"
25952595
df = DataFrame({"old_name": [1, 2, 3], "some_other_name": [4, 5, 6]})
2596-
writer = StataWriter(temp_file, df)
25972596

25982597
# Case 1: Key exists in _convert_dates
2599-
writer._convert_dates = {"old_name": "converted_date"}
2600-
columns = ["new_name"]
2601-
original_columns = ["old_name"]
2602-
for c, o in zip(columns, original_columns):
2603-
if c != o and o in writer._convert_dates:
2604-
writer._convert_dates[c] = writer._convert_dates[o]
2605-
del writer._convert_dates[o]
2606-
assert writer._convert_dates == {"new_name": "converted_date"}
2598+
convert_dates = {"old_name": "converted_date"}
2599+
df.rename(columns={"old_name": "new_name"}, inplace=True)
2600+
with StataWriter(
2601+
temp_file,
2602+
df,
2603+
convert_dates=convert_dates,
2604+
version=version,
2605+
) as writer:
2606+
writer.write_file()
2607+
result = read_stata(temp_file)
2608+
assert list(result.columns) == ["new_name", "some_other_name"]
2609+
assert "converted_date" in result.columns
26072610

26082611
# Case 2: Key does not exist in _convert_dates
2609-
writer._convert_dates = {"some_other_name": "converted_date"}
2610-
columns = ["new_name"]
2611-
original_columns = ["old_name"]
2612-
for c, o in zip(columns, original_columns):
2613-
if c != o and o in writer._convert_dates:
2614-
writer._convert_dates[c] = writer._convert_dates[o]
2615-
del writer._convert_dates[o]
2616-
assert writer._convert_dates == {"some_other_name": "converted_date"}
2612+
df = DataFrame({"old_name": [1, 2, 3], "some_other_name": [4, 5, 6]})
2613+
convert_dates = {"some_other_name": "converted_date"}
2614+
df.rename(columns={"old_name": "new_name"}, inplace=True)
2615+
with StataWriter(
2616+
temp_file,
2617+
df,
2618+
convert_dates=convert_dates,
2619+
version=version,
2620+
) as writer:
2621+
writer.write_file()
2622+
result = read_stata(temp_file)
2623+
assert list(result.columns) == ["new_name", "some_other_name"]
2624+
assert "converted_date" in result.columns

0 commit comments

Comments
 (0)