@@ -648,33 +648,34 @@ def test_tz_localize_t2d(self):
648
648
)
649
649
def test_coerce_fallback (self , error ):
650
650
# GH#46071
651
+ # 2 valid dates with different formats
652
+ # Should parse with no errors
651
653
s = pd .Series (["6/30/2025" , "1 27 2024" ])
652
654
expected = pd .Series (
653
655
[pd .Timestamp ("2025-06-30 00:00:00" ), pd .Timestamp ("2024-01-27 00:00:00" )]
654
656
)
655
-
656
657
result = pd .to_datetime (s , errors = error , infer_datetime_format = True )
657
-
658
- if error == "coerce" :
659
- assert result [1 ] is not pd .NaT
660
-
661
658
tm .assert_series_equal (expected , result )
662
659
660
+ # Invalid inputs
661
+ # Errors should be raised for the second element
663
662
expected2 = pd .Series ([pd .Timestamp ("2000-01-01 00:00:00" ), pd .NaT ])
664
-
663
+ # Out of bounds date
665
664
es1 = pd .Series (["1/1/2000" , "7/12/1200" ])
666
- es2 = pd . Series ([ "1/1/2000" , "Hello" ])
667
-
665
+ # Invalid input string
666
+ es2 = pd . Series ([ "1/1/2000" , "Invalid input" ])
668
667
if error == "coerce" :
669
668
eres1 = pd .to_datetime (es1 , errors = error , infer_datetime_format = True )
670
- eres2 = pd .to_datetime (es2 , errors = error , infer_datetime_format = True )
671
669
tm .assert_series_equal (expected2 , eres1 )
670
+ eres2 = pd .to_datetime (es2 , errors = error , infer_datetime_format = True )
672
671
tm .assert_series_equal (expected2 , eres2 )
673
672
else :
674
673
with pytest .raises (
675
674
OutOfBoundsDatetime , match = "Out of bounds nanosecond timestamp"
676
675
):
677
676
pd .to_datetime (es1 , errors = error , infer_datetime_format = True )
678
677
679
- with pytest .raises (ParserError , match = "Unknown string format: Hello" ):
678
+ with pytest .raises (
679
+ ParserError , match = "Unknown string format: Invalid input"
680
+ ):
680
681
pd .to_datetime (es2 , errors = error , infer_datetime_format = True )
0 commit comments