Skip to content

Commit 49f99ac

Browse files
wcwagnerjreback
authored andcommitted
BUG: Fixed float parsing with unit when using pd.to_datetime (GH13834)
closes #13834 Author: wcwagner <[email protected]> Closes #13847 from wcwagner/bug/13834 and squashes the following commits: 54a26ee [wcwagner] BUG: Fixed float parsing with unit when using pd.to_datetime (GH13834)
1 parent ae26ec7 commit 49f99ac

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

doc/source/whatsnew/v0.19.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -972,3 +972,4 @@ Bug Fixes
972972

973973
- Bug in ``Index`` raises ``KeyError`` displaying incorrect column when column is not in the df and columns contains duplicate values (:issue:`13822`)
974974
- Bug in ``Period`` and ``PeriodIndex`` creating wrong dates when frequency has combined offset aliases (:issue:`13874`)
975+
- Bug in ``pd.to_datetime()`` did not cast floats correctly when ``unit`` was specified, resulting in truncated datetime (:issue:`13845`)

pandas/tseries/tests/test_timeseries.py

+8
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,14 @@ def test_to_datetime_unit(self):
745745
seconds=t) for t in range(20)] + [NaT])
746746
assert_series_equal(result, expected)
747747

748+
# GH13834
749+
s = Series([epoch + t for t in np.arange(0, 2, .25)] +
750+
[iNaT]).astype(float)
751+
result = to_datetime(s, unit='s')
752+
expected = Series([Timestamp('2013-06-09 02:42:28') + timedelta(
753+
seconds=t) for t in np.arange(0, 2, .25)] + [NaT])
754+
assert_series_equal(result, expected)
755+
748756
s = concat([Series([epoch + t for t in range(20)]
749757
).astype(float), Series([np.nan])],
750758
ignore_index=True)

pandas/tslib.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -2095,7 +2095,7 @@ cpdef array_with_unit_to_datetime(ndarray values, unit, errors='coerce'):
20952095
# if we have nulls that are not type-compat
20962096
# then need to iterate
20972097
try:
2098-
iresult = values.astype('i8')
2098+
iresult = values.astype('i8', casting='same_kind', copy=False)
20992099
mask = iresult == iNaT
21002100
iresult[mask] = 0
21012101
fvalues = iresult.astype('f8') * m

0 commit comments

Comments
 (0)