Skip to content

Commit dc4944d

Browse files
committed
BUG: fix to_datetime to handle int16 and int8
Fixes #13451
1 parent 013c2ce commit dc4944d

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

doc/source/whatsnew/v0.18.2.txt

+2
Original file line numberDiff line numberDiff line change
@@ -517,3 +517,5 @@ Bug Fixes
517517

518518

519519
- Bug in ``Categorical.remove_unused_categories()`` changes ``.codes`` dtype to platform int (:issue:`13261`)
520+
521+
- Bug in ``DataFrame.to_datetime()`` raises ValueError in case of dtype ``int8`` and ``int16`` (:issue:`13451`)

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,11 @@ 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+
# prevent overflow in case of int8 or int16
513+
if com.is_integer_dtype(values):
514+
values = values.astype('int64', copy=False)
515+
return values
512516

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

0 commit comments

Comments
 (0)