Skip to content
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

BUG/API: _values_for_factorize/_from_factorized round-trip #32798

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pandas/core/arrays/boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,8 @@ def _values_for_factorize(self) -> Tuple[np.ndarray, int]:

@classmethod
def _from_factorized(cls, values, original: "BooleanArray") -> "BooleanArray":
return cls._from_sequence(values, dtype=original.dtype)
mask = values == -1
return cls(values.astype(bool), mask)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The astype causes a copy, which I think wasn't present before. This could perhaps be a .view('bool') to preserve the previous no-copy behavior.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed to .astype(bool, copy=False)


_HANDLED_TYPES = (np.ndarray, numbers.Number, bool, np.bool_)

Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/extension/base/reshaping.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,10 @@ def test_transpose(self, data):
self.assert_frame_equal(result, expected)
self.assert_frame_equal(np.transpose(np.transpose(df)), df)
self.assert_frame_equal(np.transpose(np.transpose(df[["A"]])), df[["A"]])

def test_factorize_roundtrip(self, data):
# GH#32673
values = data._values_for_factorize()[0]
result = type(data)._from_factorized(values, data)

self.assert_equal(result, data)
4 changes: 3 additions & 1 deletion pandas/tests/extension/json/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ def _from_sequence(cls, scalars, dtype=None, copy=False):

@classmethod
def _from_factorized(cls, values, original):
return cls([UserDict(x) for x in values if x != ()])
return cls(
[UserDict(x) if x != () else original.dtype.na_value for x in values]
)

def __getitem__(self, item):
if isinstance(item, numbers.Integral):
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/extension/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pandas.core.dtypes.dtypes import DatetimeTZDtype

import pandas as pd
import pandas._testing as tm
from pandas.core.arrays import DatetimeArray
from pandas.tests.extension import base

Expand Down Expand Up @@ -201,6 +202,13 @@ def test_unstack(self, obj):
result = ser.unstack(0)
self.assert_equal(result, expected)

def test_factorize_roundtrip(self, data):
# GH#32673, for DTA we dont preserve freq
values = data._values_for_factorize()[0]
result = type(data)._from_factorized(values, data)

tm.assert_numpy_array_equal(result.asi8, data.asi8)


class TestSetitem(BaseDatetimeTests, base.BaseSetitemTests):
pass
Expand Down