Skip to content

Commit 806bb20

Browse files
Jason MokJason Mok
Jason Mok
authored and
Jason Mok
committed
Implement tests for GH #55431
1 parent 84e50f5 commit 806bb20

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

pandas/tests/reshape/test_cut.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,3 +789,31 @@ def test_cut_with_nullable_int64():
789789
result = cut(series, bins=bins)
790790

791791
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"

reproduce_issue.py

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)