@@ -1782,7 +1782,8 @@ class TestToDatetimeUnit:
17821782 def test_to_datetime_month_or_year_unit_int (self , cache , unit , item , request ):
17831783 # GH#50870 Note we have separate tests that pd.Timestamp gets these right
17841784 ts = Timestamp (item , unit = unit )
1785- expected = DatetimeIndex ([ts ], dtype = "M8[ns]" )
1785+ dtype = "M8[ns]" if isinstance (item , float ) else "M8[s]"
1786+ expected = DatetimeIndex ([ts ], dtype = dtype )
17861787
17871788 result = to_datetime ([item ], unit = unit , cache = cache )
17881789 tm .assert_index_equal (result , expected )
@@ -1796,7 +1797,7 @@ def test_to_datetime_month_or_year_unit_int(self, cache, unit, item, request):
17961797 # with a nan!
17971798 result = to_datetime (np .array ([item , np .nan ]), unit = unit , cache = cache )
17981799 assert result .isna ()[1 ]
1799- tm .assert_index_equal (result [:1 ], expected )
1800+ tm .assert_index_equal (result [:1 ], expected . astype ( "M8[ns]" ) )
18001801
18011802 @pytest .mark .parametrize ("unit" , ["Y" , "M" ])
18021803 def test_to_datetime_month_or_year_unit_non_round_float (self , cache , unit ):
@@ -1820,12 +1821,12 @@ def test_to_datetime_month_or_year_unit_non_round_float(self, cache, unit):
18201821 # In 3.0, the string "1.5" is parsed as as it would be without unit,
18211822 # which fails. With errors="coerce" this becomes NaT.
18221823 res = to_datetime (["1.5" ], unit = unit , errors = "coerce" )
1823- expected = to_datetime ([NaT ]). as_unit ( "ns" )
1824+ expected = to_datetime ([NaT ])
18241825 tm .assert_index_equal (res , expected )
18251826
18261827 # round floats are OK
18271828 res = to_datetime ([1.0 ], unit = unit )
1828- expected = to_datetime ([1 ], unit = unit )
1829+ expected = to_datetime ([1 ], unit = unit ). as_unit ( "ns" )
18291830 tm .assert_index_equal (res , expected )
18301831
18311832 def test_unit (self , cache ):
@@ -1853,7 +1854,7 @@ def test_unit_array_mixed_nans_large_int(self, cache):
18531854 values = [1420043460000000000000000 , iNaT , NaT , np .nan , "NaT" ]
18541855
18551856 result = to_datetime (values , errors = "coerce" , unit = "s" , cache = cache )
1856- expected = DatetimeIndex (["NaT" , "NaT" , "NaT" , "NaT" , "NaT" ], dtype = "M8[ns ]" )
1857+ expected = DatetimeIndex (["NaT" , "NaT" , "NaT" , "NaT" , "NaT" ], dtype = "M8[s ]" )
18571858 tm .assert_index_equal (result , expected )
18581859
18591860 msg = "cannot convert input 1420043460000000000000000 with the unit 's'"
@@ -1950,12 +1951,13 @@ def test_to_datetime_unit(self, dtype):
19501951 epoch = 1370745748
19511952 ser = Series ([epoch + t for t in range (20 )]).astype (dtype )
19521953 result = to_datetime (ser , unit = "s" )
1954+ unit = "s" if dtype is int else "ns"
19531955 expected = Series (
19541956 [
19551957 Timestamp ("2013-06-09 02:42:28" ) + timedelta (seconds = t )
19561958 for t in range (20 )
19571959 ],
1958- dtype = "M8[ns ]" ,
1960+ dtype = f "M8[{ unit } ]" ,
19591961 )
19601962 tm .assert_series_equal (result , expected )
19611963
@@ -1964,10 +1966,13 @@ def test_to_datetime_unit_with_nulls(self, null):
19641966 epoch = 1370745748
19651967 ser = Series ([epoch + t for t in range (20 )] + [null ])
19661968 result = to_datetime (ser , unit = "s" )
1969+ # With np.nan, the list gets cast to a float64 array, which always
1970+ # gets ns unit.
1971+ unit = "ns" if null is np .nan else "s"
19671972 expected = Series (
19681973 [Timestamp ("2013-06-09 02:42:28" ) + timedelta (seconds = t ) for t in range (20 )]
19691974 + [NaT ],
1970- dtype = "M8[ns ]" ,
1975+ dtype = f "M8[{ unit } ]" ,
19711976 )
19721977 tm .assert_series_equal (result , expected )
19731978
@@ -1992,25 +1997,25 @@ def test_to_datetime_unit_na_values(self):
19921997 result = to_datetime ([1 , 2 , "NaT" , NaT , np .nan ], unit = "D" )
19931998 expected = DatetimeIndex (
19941999 [Timestamp ("1970-01-02" ), Timestamp ("1970-01-03" )] + ["NaT" ] * 3 ,
1995- dtype = "M8[ns ]" ,
2000+ dtype = "M8[s ]" ,
19962001 )
19972002 tm .assert_index_equal (result , expected )
19982003
1999- @pytest .mark .parametrize ("bad_val" , ["foo" , 111111111 ])
2004+ @pytest .mark .parametrize ("bad_val" , ["foo" , 111111111111111 ])
20002005 def test_to_datetime_unit_invalid (self , bad_val ):
20012006 if bad_val == "foo" :
20022007 msg = f"Unknown datetime string format, unable to parse: { bad_val } "
20032008 else :
2004- msg = "cannot convert input 111111111 with the unit 'D'"
2009+ msg = "cannot convert input 111111111111111 with the unit 'D'"
20052010 with pytest .raises (ValueError , match = msg ):
20062011 to_datetime ([1 , 2 , bad_val ], unit = "D" )
20072012
2008- @pytest .mark .parametrize ("bad_val" , ["foo" , 111111111 ])
2013+ @pytest .mark .parametrize ("bad_val" , ["foo" , 111111111111111 ])
20092014 def test_to_timestamp_unit_coerce (self , bad_val ):
20102015 # coerce we can process
20112016 expected = DatetimeIndex (
20122017 [Timestamp ("1970-01-02" ), Timestamp ("1970-01-03" )] + ["NaT" ] * 1 ,
2013- dtype = "M8[ns ]" ,
2018+ dtype = "M8[s ]" ,
20142019 )
20152020 result = to_datetime ([1 , 2 , bad_val ], unit = "D" , errors = "coerce" )
20162021 tm .assert_index_equal (result , expected )
@@ -3223,7 +3228,7 @@ def test_unix(self):
32233228 result = Series (to_datetime ([0 , 1 , 2 ], unit = "D" , origin = "unix" ))
32243229 expected = Series (
32253230 [Timestamp ("1970-01-01" ), Timestamp ("1970-01-02" ), Timestamp ("1970-01-03" )],
3226- dtype = "M8[ns ]" ,
3231+ dtype = "M8[s ]" ,
32273232 )
32283233 tm .assert_series_equal (result , expected )
32293234
@@ -3262,8 +3267,10 @@ def test_invalid_origin(self, unit):
32623267 def test_epoch (self , units , epochs ):
32633268 epoch_1960 = Timestamp (1960 , 1 , 1 )
32643269 units_from_epochs = np .arange (5 , dtype = np .int64 )
3270+ exp_unit = "s" if units == "D" else units
32653271 expected = Series (
3266- [pd .Timedelta (x , unit = units ) + epoch_1960 for x in units_from_epochs ]
3272+ [pd .Timedelta (x , unit = units ) + epoch_1960 for x in units_from_epochs ],
3273+ dtype = f"M8[{ exp_unit } ]" ,
32673274 )
32683275
32693276 result = Series (to_datetime (units_from_epochs , unit = units , origin = epochs ))
@@ -3358,7 +3365,7 @@ def test_arg_tz_ns_unit(self, offset, utc, exp):
33583365 # GH 25546
33593366 arg = "2019-01-01T00:00:00.000" + offset
33603367 result = to_datetime ([arg ], unit = "ns" , utc = utc )
3361- expected = to_datetime ([exp ]).as_unit ("ns " )
3368+ expected = to_datetime ([exp ]).as_unit ("us " )
33623369 tm .assert_index_equal (result , expected )
33633370
33643371
@@ -3458,7 +3465,7 @@ def test_empty_string_datetime_coerce__unit():
34583465 # GH13044
34593466 # coerce empty string to pd.NaT
34603467 result = to_datetime ([1 , "" ], unit = "s" , errors = "coerce" )
3461- expected = DatetimeIndex (["1970-01-01 00:00:01" , "NaT" ], dtype = "datetime64[ns ]" )
3468+ expected = DatetimeIndex (["1970-01-01 00:00:01" , "NaT" ], dtype = "datetime64[s ]" )
34623469 tm .assert_index_equal (expected , result )
34633470
34643471 # verify that no exception is raised even when errors='raise' is set
0 commit comments