Skip to content

Commit 0826212

Browse files
committed
Fix for pandas issue pandas-dev#56929, including new test
1 parent 7368686 commit 0826212

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

pandas/_libs/parsers.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ cdef class TextReader:
746746
is not None else 0)
747747

748748
# if wrong number of blanks or no index, not our format
749-
if (lc != unnamed_count and lc - ic > unnamed_count) or ic == 0:
749+
if (lc != unnamed_count and lc - ic >= unnamed_count) or ic == 0:
750750
hr -= 1
751751
self.parser_start -= 1
752752
this_header = [None] * lc

pandas/tests/io/formats/test_to_csv.py

+16
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,22 @@ def test_to_csv_float_ea_no_float_format(self):
332332
)
333333
assert result == expected
334334

335+
def test_to_csv_multi_index_nan(self):
336+
# Create a MultiIndex DataFrame
337+
columns = pd.MultiIndex.from_tuples([('Level 1', 'Level 2')], names=['level1', 'level2'])
338+
data = [[np.nan], [0.1], [0.4]]
339+
df_complex = pd.DataFrame(data, columns=columns)
340+
341+
# Expected DataFrame
342+
expected_df = pd.DataFrame(data, columns=columns, index=range(3))
343+
344+
# Save and load the DataFrame as a CSV
345+
with tm.ensure_clean("complex_data.csv") as path:
346+
df_complex.to_csv(path)
347+
loaded_df_complex = pd.read_csv(path, header=[0, 1], index_col=0)
348+
349+
tm.assert_frame_equal(loaded_df_complex, expected_df)
350+
335351
def test_to_csv_multi_index(self):
336352
# see gh-6618
337353
df = DataFrame([1], columns=pd.MultiIndex.from_arrays([[1], [2]]))

0 commit comments

Comments
 (0)