Skip to content

Fix KeyError by adding check for _convert_dates #60539

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion pandas/io/stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2611,7 +2611,7 @@ def _check_column_names(self, data: DataFrame) -> DataFrame:
# Check date conversion, and fix key if needed
if self._convert_dates:
for c, o in zip(columns, original_columns):
if c != o:
if c != o and o in self._convert_dates:
self._convert_dates[c] = self._convert_dates[o]
del self._convert_dates[o]

Expand Down
27 changes: 27 additions & 0 deletions pandas/tests/io/test_stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2587,3 +2587,30 @@ def test_many_strl(temp_file, version):
lbls = ["".join(v) for v in itertools.product(*([string.ascii_letters] * 3))]
value_labels = {"col": {i: lbls[i] for i in range(n)}}
df.to_stata(temp_file, value_labels=value_labels, version=version)


@pytest.mark.parametrize("version", [114, 117, 118, 119, None])
def test_convert_dates_key_handling(tmp_path, version):
temp_file = tmp_path / "test.dta"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make the first line of test references the GitHub issue (or PR):

def test_convert_dates_key_handling(tmp_path, version):
    # GH#60536
    temp_file = tmp_path / "test.dta"

df = DataFrame({"old_name": [1, 2, 3], "some_other_name": [4, 5, 6]})
writer = StataWriter(temp_file, df)

# Case 1: Key exists in _convert_dates
writer._convert_dates = {"old_name": "converted_date"}
columns = ["new_name"]
original_columns = ["old_name"]
for c, o in zip(columns, original_columns):
if c != o and o in writer._convert_dates:
writer._convert_dates[c] = writer._convert_dates[o]
del writer._convert_dates[o]
assert writer._convert_dates == {"new_name": "converted_date"}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you instead test using the public API - provide a DataFrame with an name that will be changed, and a convert_dates, then call read_stata to check the result.


# Case 2: Key does not exist in _convert_dates
writer._convert_dates = {"some_other_name": "converted_date"}
columns = ["new_name"]
original_columns = ["old_name"]
for c, o in zip(columns, original_columns):
if c != o and o in writer._convert_dates:
writer._convert_dates[c] = writer._convert_dates[o]
del writer._convert_dates[o]
assert writer._convert_dates == {"some_other_name": "converted_date"}