Skip to content

BUG: Fix na_values dict not working on index column (#57547) #57965

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

Merged
merged 1 commit into from
Apr 9, 2024
Merged
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 @@ -301,6 +301,7 @@ Bug fixes
- Fixed bug in :meth:`Series.diff` allowing non-integer values for the ``periods`` argument. (:issue:`56607`)
- Fixed bug in :meth:`Series.rank` that doesn't preserve missing values for nullable integers when ``na_option='keep'``. (:issue:`56976`)
- Fixed bug in :meth:`Series.replace` and :meth:`DataFrame.replace` inconsistently replacing matching instances when ``regex=True`` and missing values are present. (:issue:`56599`)
- Fixed bug in :meth:`read_csv` raising ``TypeError`` when ``index_col`` is specified and ``na_values`` is a dict containing the key ``None``. (:issue:`57547`)

Categorical
^^^^^^^^^^^
Expand Down
2 changes: 2 additions & 0 deletions pandas/io/parsers/base_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,8 @@ def _agg_index(self, index, try_parse_dates: bool = True) -> Index:
col_na_values, col_na_fvalues = _get_na_values(
col_name, self.na_values, self.na_fvalues, self.keep_default_na
)
else:
col_na_values, col_na_fvalues = set(), set()

clean_dtypes = self._clean_mapping(self.dtype)

Expand Down
13 changes: 7 additions & 6 deletions pandas/io/parsers/python_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,14 +356,15 @@ def _convert_data(

if isinstance(self.na_values, dict):
for col in self.na_values:
na_value = self.na_values[col]
na_fvalue = self.na_fvalues[col]
if col is not None:
na_value = self.na_values[col]
na_fvalue = self.na_fvalues[col]

if isinstance(col, int) and col not in self.orig_names:
col = self.orig_names[col]
if isinstance(col, int) and col not in self.orig_names:
col = self.orig_names[col]

clean_na_values[col] = na_value
clean_na_fvalues[col] = na_fvalue
clean_na_values[col] = na_value
clean_na_fvalues[col] = na_fvalue
else:
clean_na_values = self.na_values
clean_na_fvalues = self.na_fvalues
Expand Down
41 changes: 41 additions & 0 deletions pandas/tests/io/parser/test_na_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,47 @@ def test_na_values_dict_aliasing(all_parsers):
tm.assert_dict_equal(na_values, na_values_copy)


def test_na_values_dict_null_column_name(all_parsers):
# see gh-57547
parser = all_parsers
data = ",x,y\n\nMA,1,2\nNA,2,1\nOA,,3"
names = [None, "x", "y"]
na_values = {name: STR_NA_VALUES for name in names}
dtype = {None: "object", "x": "float64", "y": "float64"}

if parser.engine == "pyarrow":
msg = "The pyarrow engine doesn't support passing a dict for na_values"
with pytest.raises(ValueError, match=msg):
parser.read_csv(
StringIO(data),
index_col=0,
header=0,
dtype=dtype,
names=names,
na_values=na_values,
keep_default_na=False,
)
return

expected = DataFrame(
{None: ["MA", "NA", "OA"], "x": [1.0, 2.0, np.nan], "y": [2.0, 1.0, 3.0]}
)

expected = expected.set_index(None)

result = parser.read_csv(
StringIO(data),
index_col=0,
header=0,
dtype=dtype,
names=names,
na_values=na_values,
keep_default_na=False,
)

tm.assert_frame_equal(result, expected)


def test_na_values_dict_col_index(all_parsers):
# see gh-14203
data = "a\nfoo\n1"
Expand Down