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 @@ -504,6 +504,7 @@ Bug Fixes
504
504
505
505
506
506
- 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`)
507
508
- Bug in extension dtype creation where the created types were not is/identical (:issue:`13285`)
508
509
509
510
- Bug in ``NaT`` - ``Period`` raises ``AttributeError`` (:issue:`13071`)
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,12 @@ 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
+
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
512
517
513
518
values = (coerce (arg [unit_rev ['year' ]]) * 10000 +
514
519
coerce (arg [unit_rev ['month' ]]) * 100 +
You can’t perform that action at this time.
0 commit comments