diff --git a/pandas/tests/io/parser/test_parse_dates.py b/pandas/tests/io/parser/test_parse_dates.py index c0b29d5019675..a73768e9ccca7 100644 --- a/pandas/tests/io/parser/test_parse_dates.py +++ b/pandas/tests/io/parser/test_parse_dates.py @@ -56,6 +56,40 @@ def test_separator_date_conflict(all_parsers): tm.assert_frame_equal(df, expected) +def test_read_csv_with_custom_date_parser(all_parsers): + # 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 = all_parsers.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): data = """\ diff --git a/pandas/tests/series/test_dtypes.py b/pandas/tests/series/test_dtypes.py index d59f0c05c7462..ac9ac6c040d6d 100644 --- a/pandas/tests/series/test_dtypes.py +++ b/pandas/tests/series/test_dtypes.py @@ -111,4 +111,17 @@ def test_reindex_astype_order_consistency(self): new_dtype = str s1 = s.reindex(new_index).astype(temp_dtype).astype(new_dtype) s2 = s.astype(temp_dtype).reindex(new_index).astype(new_dtype) - tm.assert_series_equal(s1, s2) + tm.assert_series_equal(s1, s2 + + def test_series_with_object_dtype(self): + # GH 21881 + timestamp = pd.Timestamp(1412526600000000000) + series = pd.Series([], dtype=object) + series['timestamp'] = timestamp + expected = type(series.timestamp) + + series = pd.Series([], dtype=object) + series['anything'] = 300.0 + series['timestamp'] = timestamp + result = type(series.timestamp) + assert result == expected