Skip to content

Commit 9d33c7b

Browse files
ravinimmijreback
authored andcommitted
BUG: fix to_datetime to handle int16 and int8
closes #13451 Author: Ravi Kumar Nimmi <[email protected]> Closes #13464 from ravinimmi/bugfix and squashes the following commits: dc4944d [Ravi Kumar Nimmi] BUG: fix to_datetime to handle int16 and int8
1 parent 883df65 commit 9d33c7b

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

doc/source/whatsnew/v0.18.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ Bug Fixes
504504

505505

506506
- Bug in ``pd.to_datetime()`` when passing invalid datatypes (e.g. bool); will now respect the ``errors`` keyword (:issue:`13176`)
507+
- Bug in ``pd.to_datetime()`` which overflowed on ``int8``, `int16`` dtypes (:issue:`13451`)
507508
- Bug in extension dtype creation where the created types were not is/identical (:issue:`13285`)
508509

509510
- Bug in ``NaT`` - ``Period`` raises ``AttributeError`` (:issue:`13071`)

pandas/tseries/tests/test_timeseries.py

+27
Original file line numberDiff line numberDiff line change
@@ -2563,6 +2563,33 @@ def test_dataframe(self):
25632563
with self.assertRaises(ValueError):
25642564
to_datetime(df2)
25652565

2566+
def test_dataframe_dtypes(self):
2567+
# #13451
2568+
df = DataFrame({'year': [2015, 2016],
2569+
'month': [2, 3],
2570+
'day': [4, 5]})
2571+
2572+
# int16
2573+
result = to_datetime(df.astype('int16'))
2574+
expected = Series([Timestamp('20150204 00:00:00'),
2575+
Timestamp('20160305 00:00:00')])
2576+
assert_series_equal(result, expected)
2577+
2578+
# mixed dtypes
2579+
df['month'] = df['month'].astype('int8')
2580+
df['day'] = df['day'].astype('int8')
2581+
result = to_datetime(df)
2582+
expected = Series([Timestamp('20150204 00:00:00'),
2583+
Timestamp('20160305 00:00:00')])
2584+
assert_series_equal(result, expected)
2585+
2586+
# float
2587+
df = DataFrame({'year': [2000, 2001],
2588+
'month': [1.5, 1],
2589+
'day': [1, 1]})
2590+
with self.assertRaises(ValueError):
2591+
to_datetime(df)
2592+
25662593

25672594
class TestDatetimeIndex(tm.TestCase):
25682595
_multiprocess_can_split_ = True

pandas/tseries/tools.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,12 @@ def f(value):
508508

509509
def coerce(values):
510510
# we allow coercion to if errors allows
511-
return to_numeric(values, errors=errors)
511+
values = to_numeric(values, errors=errors)
512+
513+
# prevent overflow in case of int8 or int16
514+
if com.is_integer_dtype(values):
515+
values = values.astype('int64', copy=False)
516+
return values
512517

513518
values = (coerce(arg[unit_rev['year']]) * 10000 +
514519
coerce(arg[unit_rev['month']]) * 100 +

0 commit comments

Comments
 (0)