-
Notifications
You must be signed in to change notification settings - Fork 6
fix: use public pandas APIs where possible #60
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
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4f96f36
refactor: use public pandas APIs where possible
tswast eff42ed
no need to override take
tswast fa81f71
Merge remote-tracking branch 'upstream/main' into issue28-use-public-…
tswast 60da4d0
backport take implementation
tswast 66d5b66
move remaining private pandas methods to backports
tswast a4e89c2
add note about _validate_scalar to docstring
tswast 5de6407
comment why we can't use public mixin
tswast 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 |
---|---|---|
|
@@ -20,15 +20,32 @@ | |
""" | ||
|
||
import operator | ||
from typing import Any | ||
|
||
import numpy | ||
import packaging.version | ||
import pandas | ||
from pandas._libs.lib import is_integer | ||
from pandas.api.types import is_integer | ||
import pandas.compat.numpy.function | ||
import pandas.core.nanops | ||
|
||
|
||
pandas_release = packaging.version.parse(pandas.__version__).release | ||
|
||
# Create aliases for private methods in case they move in a future version. | ||
nanall = pandas.core.nanops.nanall | ||
nanany = pandas.core.nanops.nanany | ||
nanmax = pandas.core.nanops.nanmax | ||
nanmin = pandas.core.nanops.nanmin | ||
numpy_validate_all = pandas.compat.numpy.function.validate_all | ||
numpy_validate_any = pandas.compat.numpy.function.validate_any | ||
numpy_validate_max = pandas.compat.numpy.function.validate_max | ||
numpy_validate_min = pandas.compat.numpy.function.validate_min | ||
|
||
if pandas_release >= (1, 2): | ||
nanmedian = pandas.core.nanops.nanmedian | ||
numpy_validate_median = pandas.compat.numpy.function.validate_median | ||
|
||
|
||
def import_default(module_name, force=False, default=None): | ||
""" | ||
|
@@ -55,7 +72,7 @@ def import_default(module_name, force=False, default=None): | |
return getattr(module, name, default) | ||
|
||
|
||
@import_default("pandas.core.arraylike") | ||
@import_default("pandas.core.arraylike") # TODO: is there a public API for this? | ||
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. Oops, need to handle this TODO |
||
class OpsMixin: | ||
def _cmp_method(self, other, op): # pragma: NO COVER | ||
return NotImplemented | ||
|
@@ -81,6 +98,8 @@ def __ge__(self, other): | |
__add__ = __radd__ = __sub__ = lambda self, other: NotImplemented | ||
|
||
|
||
# TODO: use public API if possible | ||
# https://github.com/pandas-dev/pandas/pull/45544/files | ||
@import_default("pandas.core.arrays._mixins", pandas_release < (1, 3)) | ||
class NDArrayBackedExtensionArray(pandas.core.arrays.base.ExtensionArray): | ||
|
||
|
@@ -130,6 +149,28 @@ def copy(self): | |
def repeat(self, n): | ||
return self.__class__(self._ndarray.repeat(n), self._dtype) | ||
|
||
def take( | ||
self, | ||
indices, | ||
*, | ||
allow_fill: bool = False, | ||
fill_value: Any = None, | ||
axis: int = 0, | ||
): | ||
from pandas.core.algorithms import take | ||
|
||
if allow_fill: | ||
fill_value = self._validate_scalar(fill_value) | ||
|
||
new_data = take( | ||
self._ndarray, | ||
indices, | ||
allow_fill=allow_fill, | ||
fill_value=fill_value, | ||
axis=axis, | ||
) | ||
return self._from_backing_data(new_data) | ||
|
||
@classmethod | ||
def _concat_same_type(cls, to_concat, axis=0): | ||
dtypes = {str(x.dtype) for x in to_concat} | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.