File tree 3 files changed +34
-1
lines changed
3 files changed +34
-1
lines changed Original file line number Diff line number Diff line change @@ -517,3 +517,5 @@ Bug Fixes
517
517
518
518
519
519
- 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`)
Original file line number Diff line number Diff line change @@ -2563,6 +2563,33 @@ def test_dataframe(self):
2563
2563
with self .assertRaises (ValueError ):
2564
2564
to_datetime (df2 )
2565
2565
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
+
2566
2593
2567
2594
class TestDatetimeIndex (tm .TestCase ):
2568
2595
_multiprocess_can_split_ = True
Original file line number Diff line number Diff line change @@ -508,7 +508,11 @@ def f(value):
508
508
509
509
def coerce (values ):
510
510
# 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
512
516
513
517
values = (coerce (arg [unit_rev ['year' ]]) * 10000 +
514
518
coerce (arg [unit_rev ['month' ]]) * 100 +
You can’t perform that action at this time.
0 commit comments