Skip to content

BUG: read_csv with custom date parser and na_filter=True results in ValueError #39079

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 16 commits into from
33 changes: 33 additions & 0 deletions pandas/tests/io/parser/test_parse_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,39 @@ def test_separator_date_conflict(all_parsers):
)
tm.assert_frame_equal(df, expected)

def test_read_csv_with_custom_date_parser(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

use the allparser fixture (and thus parser.read_csv) see the test above for how

# GH36111
def __custom_date_parser(time):
time = time.astype(np.float)
time = time.astype(np.int) # convert float seconds to int type
return pd.to_timedelta(time, unit="s")
testdata = StringIO(
"""time e n h
41047.00 -98573.7297 871458.0640 389.0089
41048.00 -98573.7299 871458.0640 389.0089
41049.00 -98573.7300 871458.0642 389.0088
41050.00 -98573.7299 871458.0643 389.0088
41051.00 -98573.7302 871458.0640 389.0086
"""
)

result = pd.read_csv(
testdata,
delim_whitespace=True,
parse_dates=True,
date_parser=__custom_date_parser,
index_col="time",
)
expected = pd.DataFrame(
{
"e": [-98573.7297, -98573.7299, -98573.7300, -98573.7299, -98573.7302],
"n": [871458.0640, 871458.0640, 871458.0642, 871458.0643, 871458.0640],
"h": [389.0089, 389.0089, 389.0088, 389.0088, 389.0086],
},
index=[41047, 41048, 41049, 41050, 41051],
)
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize("keep_date_col", [True, False])
def test_multiple_date_col_custom(all_parsers, keep_date_col):
Expand Down