@@ -789,3 +789,31 @@ def test_cut_with_nullable_int64():
789
789
result = cut (series , bins = bins )
790
790
791
791
tm .assert_series_equal (result , expected )
792
+
793
+ def test_datetime_cut_notna ():
794
+ """
795
+ Ensure pd.cut can handle DatetimeArray without raising an AttributeError
796
+ related to missing 'notna' method.
797
+
798
+ GH 55431: Tests for AttributeError when using pd.cut on datetime data.
799
+ """
800
+ data = to_datetime (["2023-09-17" , "2023-10-06" ])
801
+ datetime_array = data .array
802
+
803
+ bins = pd .date_range (start = "2023-09-16" , periods = 3 , freq = '10D' )
804
+
805
+ # Use pd.cut to categorize datetime data and capture the result
806
+ result = pd .cut (datetime_array , bins = bins )
807
+
808
+ expected_intervals = IntervalIndex .from_arrays (
809
+ [pd .Timestamp ('2023-09-16' ), pd .Timestamp ('2023-09-26' )],
810
+ [pd .Timestamp ('2023-09-26' ), pd .Timestamp ('2023-10-06' )],
811
+ closed = 'right'
812
+ )
813
+
814
+ expected = Series (expected_intervals ).astype (CategoricalDtype (ordered = True ))
815
+
816
+ tm .assert_categorical_equal (result .categories , expected .values )
817
+
818
+ assert not hasattr (result , 'notna' ), "AttributeError related to 'notna' should not be present"
819
+ assert len (result .categories ) == 2 , "There should be exactly 2 bins"
0 commit comments