Skip to content

Commit 3ff5192

Browse files
authored
TST: read_csv with custom date parser and na_filter=True results in ValueError (#39496)
1 parent 7abac27 commit 3ff5192

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

pandas/tests/io/parser/test_parse_dates.py

+37
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,43 @@
3434
date_strategy = st.datetimes()
3535

3636

37+
def test_read_csv_with_custom_date_parser(all_parsers):
38+
# GH36111
39+
def __custom_date_parser(time):
40+
time = time.astype(np.float)
41+
time = time.astype(np.int) # convert float seconds to int type
42+
return pd.to_timedelta(time, unit="s")
43+
44+
testdata = StringIO(
45+
"""time e n h
46+
41047.00 -98573.7297 871458.0640 389.0089
47+
41048.00 -98573.7299 871458.0640 389.0089
48+
41049.00 -98573.7300 871458.0642 389.0088
49+
41050.00 -98573.7299 871458.0643 389.0088
50+
41051.00 -98573.7302 871458.0640 389.0086
51+
"""
52+
)
53+
result = all_parsers.read_csv(
54+
testdata,
55+
delim_whitespace=True,
56+
parse_dates=True,
57+
date_parser=__custom_date_parser,
58+
index_col="time",
59+
)
60+
time = [41047, 41048, 41049, 41050, 41051]
61+
time = pd.TimedeltaIndex([pd.to_timedelta(i, unit="s") for i in time], name="time")
62+
expected = DataFrame(
63+
{
64+
"e": [-98573.7297, -98573.7299, -98573.7300, -98573.7299, -98573.7302],
65+
"n": [871458.0640, 871458.0640, 871458.0642, 871458.0643, 871458.0640],
66+
"h": [389.0089, 389.0089, 389.0088, 389.0088, 389.0086],
67+
},
68+
index=time,
69+
)
70+
71+
tm.assert_frame_equal(result, expected)
72+
73+
3774
def test_separator_date_conflict(all_parsers):
3875
# Regression test for gh-4678
3976
#

0 commit comments

Comments
 (0)