Skip to content

BUG: Fix reading of multi_index dataframe with NaN #57070

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 3 commits into from
Closed
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
3 changes: 2 additions & 1 deletion pandas/_libs/parsers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,8 @@ cdef class TextReader:
is not None else 0)

# if wrong number of blanks or no index, not our format
if (lc != unnamed_count and lc - ic > unnamed_count) or ic == 0:
if ((lc != unnamed_count and lc - ic >= unnamed_count) or
ic == 0):
hr -= 1
self.parser_start -= 1
this_header = [None] * lc
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/io/formats/test_to_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,24 @@ def test_to_csv_float_ea_no_float_format(self):
)
assert result == expected

def test_to_csv_multi_index_nan(self):
# Create a MultiIndex DataFrame
columns = pd.MultiIndex.from_tuples(
[("Level 1", "Level 2")], names=["level1", "level2"]
)
data = [[np.nan], [0.1], [0.4]]
df_complex = DataFrame(data, columns=columns)

# Expected DataFrame
expected_df = DataFrame(data, columns=columns, index=range(3))

# Save and load the DataFrame as a CSV
with tm.ensure_clean("complex_data.csv") as path:
df_complex.to_csv(path)
loaded_df_complex = pd.read_csv(path, header=[0, 1], index_col=0)

tm.assert_frame_equal(loaded_df_complex, expected_df)

def test_to_csv_multi_index(self):
# see gh-6618
df = DataFrame([1], columns=pd.MultiIndex.from_arrays([[1], [2]]))
Expand Down