-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
REGR: preserve reindexed array object (instead of creating new array) for concat with all-NA array #47762
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
Merged
simonjayhawkins
merged 15 commits into
pandas-dev:main
from
jorisvandenbossche:regr-concat-preserve-array-obj
Aug 30, 2022
Merged
REGR: preserve reindexed array object (instead of creating new array) for concat with all-NA array #47762
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e89624f
REGR: preserve array object for concat with all-NA array
jorisvandenbossche 7bc3b64
update PR number
jorisvandenbossche a5ae544
Merge remote-tracking branch 'upstream/main' into regr-concat-preserv…
jorisvandenbossche 40e1009
Merge remote-tracking branch 'upstream/main' into regr-concat-preserv…
jorisvandenbossche e5e19b8
Merge remote-tracking branch 'upstream/main' into regr-concat-preserv…
jorisvandenbossche c42f54e
additional test
jorisvandenbossche d76aa6f
use is_dtype_equal for compat with old numpy
jorisvandenbossche 28d72a3
Merge remote-tracking branch 'upstream/main' into regr-concat-preserv…
jorisvandenbossche 3679d63
Merge remote-tracking branch 'upstream/main' into regr-concat-preserv…
jorisvandenbossche 2e0e0a8
EA specific, simplify tests
jorisvandenbossche 12d8480
fixup
jorisvandenbossche 83b1ea9
fixup
jorisvandenbossche 2da56d5
Merge remote-tracking branch 'upstream/main' into regr-concat-preserv…
jorisvandenbossche bfba49c
Merge branch 'main' into regr-concat-preserve-array-obj
simonjayhawkins 7f25c24
Merge branch 'main' into regr-concat-preserve-array-obj
simonjayhawkins File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from pandas.tests.extension.array_with_attr.array import ( | ||
FloatAttrArray, | ||
FloatAttrDtype, | ||
) | ||
|
||
__all__ = ["FloatAttrArray", "FloatAttrDtype"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
""" | ||
Test extension array that has custom attribute information (not stored on the dtype). | ||
|
||
""" | ||
from __future__ import annotations | ||
|
||
import numbers | ||
|
||
import numpy as np | ||
|
||
from pandas._typing import type_t | ||
|
||
from pandas.core.dtypes.base import ExtensionDtype | ||
|
||
import pandas as pd | ||
from pandas.core.arrays import ExtensionArray | ||
|
||
|
||
class FloatAttrDtype(ExtensionDtype): | ||
type = float | ||
name = "float_attr" | ||
na_value = np.nan | ||
|
||
@classmethod | ||
def construct_array_type(cls) -> type_t[FloatAttrArray]: | ||
""" | ||
Return the array type associated with this dtype. | ||
|
||
Returns | ||
------- | ||
type | ||
""" | ||
return FloatAttrArray | ||
|
||
|
||
class FloatAttrArray(ExtensionArray): | ||
dtype = FloatAttrDtype() | ||
__array_priority__ = 1000 | ||
|
||
def __init__(self, values, attr=None) -> None: | ||
if not isinstance(values, np.ndarray): | ||
raise TypeError("Need to pass a numpy array of float64 dtype as values") | ||
if not values.dtype == "float64": | ||
raise TypeError("Need to pass a numpy array of float64 dtype as values") | ||
self.data = values | ||
self.attr = attr | ||
|
||
@classmethod | ||
def _from_sequence(cls, scalars, dtype=None, copy=False): | ||
data = np.array(scalars, dtype="float64", copy=copy) | ||
return cls(data) | ||
|
||
def __getitem__(self, item): | ||
if isinstance(item, numbers.Integral): | ||
return self.data[item] | ||
else: | ||
# slice, list-like, mask | ||
item = pd.api.indexers.check_array_indexer(self, item) | ||
return type(self)(self.data[item], self.attr) | ||
|
||
def __len__(self) -> int: | ||
return len(self.data) | ||
|
||
def isna(self): | ||
return np.isnan(self.data) | ||
|
||
def take(self, indexer, allow_fill=False, fill_value=None): | ||
from pandas.api.extensions import take | ||
|
||
data = self.data | ||
if allow_fill and fill_value is None: | ||
fill_value = self.dtype.na_value | ||
|
||
result = take(data, indexer, fill_value=fill_value, allow_fill=allow_fill) | ||
return type(self)(result, self.attr) | ||
|
||
def copy(self): | ||
return type(self)(self.data.copy(), self.attr) | ||
simonjayhawkins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@classmethod | ||
def _concat_same_type(cls, to_concat): | ||
data = np.concatenate([x.data for x in to_concat]) | ||
attr = to_concat[0].attr if len(to_concat) else None | ||
return cls(data, attr) |
33 changes: 33 additions & 0 deletions
33
pandas/tests/extension/array_with_attr/test_array_with_attr.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import numpy as np | ||
|
||
import pandas as pd | ||
import pandas._testing as tm | ||
from pandas.tests.extension.array_with_attr import FloatAttrArray | ||
|
||
|
||
def test_concat_with_all_na(): | ||
# https://github.com/pandas-dev/pandas/pull/47762 | ||
# ensure that attribute of the column array is preserved (when it gets | ||
# preserved in reindexing the array) during merge/concat | ||
arr = FloatAttrArray(np.array([np.nan, np.nan], dtype="float64"), attr="test") | ||
|
||
df1 = pd.DataFrame({"col": arr, "key": [0, 1]}) | ||
df2 = pd.DataFrame({"key": [0, 1], "col2": [1, 2]}) | ||
result = pd.merge(df1, df2, on="key") | ||
expected = pd.DataFrame({"col": arr, "key": [0, 1], "col2": [1, 2]}) | ||
tm.assert_frame_equal(result, expected) | ||
assert result["col"].array.attr == "test" | ||
|
||
df1 = pd.DataFrame({"col": arr, "key": [0, 1]}) | ||
df2 = pd.DataFrame({"key": [0, 2], "col2": [1, 2]}) | ||
result = pd.merge(df1, df2, on="key") | ||
expected = pd.DataFrame({"col": arr.take([0]), "key": [0], "col2": [1]}) | ||
tm.assert_frame_equal(result, expected) | ||
assert result["col"].array.attr == "test" | ||
|
||
result = pd.concat([df1.set_index("key"), df2.set_index("key")], axis=1) | ||
expected = pd.DataFrame( | ||
{"col": arr.take([0, 1, -1]), "col2": [1, np.nan, 2], "key": [0, 1, 2]} | ||
).set_index("key") | ||
tm.assert_frame_equal(result, expected) | ||
assert result["col"].array.attr == "test" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this didn't need to change? (just a leftover from the code move?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, good catch