-
-
Notifications
You must be signed in to change notification settings - Fork 18.9k
BUG: Fix all-NaT when ArrowEA.astype to categorical #62055
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 10 commits
58284dd
1896199
36b84cf
2dc0698
5987952
a5fab10
e6b7c64
95e331c
3453487
d747141
d6a5a50
0f43eca
7e5789f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -384,6 +384,18 @@ def _is_comparable_dtype(self, dtype: DtypeObj) -> bool: | |
if self.tz is not None: | ||
# If we have tz, we can compare to tzaware | ||
return isinstance(dtype, DatetimeTZDtype) | ||
|
||
from pandas import ArrowDtype | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can go at the top of the file |
||
|
||
if isinstance(dtype, ArrowDtype): | ||
import pyarrow as pa | ||
|
||
return ( | ||
pa.types.is_date32(dtype.pyarrow_dtype) | ||
or pa.types.is_date64(dtype.pyarrow_dtype) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think timestamp is comparable but date is not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The original issue was with pyarrow date dtypes, which compare fine when using astype, so I think they should be treated as comparable here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
We don't have a non-pyarrow date dtype, but if we did, it would not be considered comparable to datetime64 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think in that case the question is whether we want astype with categoricals to succeed here, or whether astype between pyarrow date and datetime64 should be disallowed for consistency There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we have analogous special-casing for the non-pyarrow dt64 that im missing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not that I know of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then I expect it shouldn’t be necessary here. I’ll take a closer look on monday There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think the relevant special-casing is in Index._maybe_downcast_for_indexing. Take a look for the inferred_type checks |
||
or pa.types.is_timestamp(dtype.pyarrow_dtype) | ||
) | ||
|
||
# if we dont have tz, we can only compare to tznaive | ||
return lib.is_np_dtype(dtype, "M") | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,14 +7,17 @@ | |
CategoricalDtype, | ||
CategoricalIndex, | ||
DatetimeIndex, | ||
Index, | ||
Interval, | ||
NaT, | ||
Period, | ||
Timestamp, | ||
array, | ||
isna, | ||
to_datetime, | ||
) | ||
import pandas._testing as tm | ||
from pandas.core.arrays.arrow.array import ArrowExtensionArray | ||
|
||
|
||
class TestAstype: | ||
|
@@ -160,3 +163,20 @@ def test_astype_category_readonly_mask_values(self): | |
result = arr.astype("category") | ||
expected = array([0, 1, 2], dtype="Int64").astype("category") | ||
tm.assert_extension_array_equal(result, expected) | ||
|
||
def test_arrow_array_astype_to_categorical_dtype_temporal(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you also test the intermediate steps that used to fail |
||
# GH#62051 | ||
pytest.importorskip("pyarrow") | ||
arr = array( | ||
["2017-01-01", "2018-01-01", "2019-01-01"], dtype="date32[day][pyarrow]" | ||
) | ||
cats = Index(["2017-01-01", "2018-01-01", "2019-01-01"], dtype="M8[s]") | ||
dtype = CategoricalDtype(categories=cats, ordered=False) | ||
|
||
assert not all(isna(arr.astype(dtype))) | ||
|
||
arr = ArrowExtensionArray._from_sequence(["1h", "2h", "3h"]) | ||
cats = Index(["1h", "2h", "3h"], dtype="m8[ns]") | ||
dtype = CategoricalDtype(cats, ordered=False) | ||
|
||
assert not all(isna(arr.astype(dtype))) |
Uh oh!
There was an error while loading. Please reload this page.