|
34 | 34 | date_strategy = st.datetimes()
|
35 | 35 |
|
36 | 36 |
|
| 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 | + |
37 | 74 | def test_separator_date_conflict(all_parsers):
|
38 | 75 | # Regression test for gh-4678
|
39 | 76 | #
|
|
0 commit comments