Skip to content

Commit cbfcd1a

Browse files
committed
updates re: ndarrybackedextensionarray
1 parent 4c522b3 commit cbfcd1a

File tree

2 files changed

+3
-95
lines changed

2 files changed

+3
-95
lines changed

db_dtypes/core.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import pandas
1919
import pandas.api.extensions
2020
from pandas.api.types import is_dtype_equal, is_list_like, is_scalar, pandas_dtype
21+
from pandas.core.arrays import _mixins
2122

2223
from db_dtypes import pandas_backports
2324

@@ -42,9 +43,7 @@ def construct_from_string(cls, name: str):
4243
return cls()
4344

4445

45-
class BaseDatetimeArray(
46-
pandas_backports.OpsMixin, pandas_backports.NDArrayBackedExtensionArray
47-
):
46+
class BaseDatetimeArray(pandas_backports.OpsMixin, _mixins.NDArrayBackedExtensionArray):
4847
# scalar used to denote NA value inside our self._ndarray, e.g. -1 for
4948
# Categorical, iNaT for Period. Outside of object dtype, self.isna() should
5049
# be exactly locations in self._ndarray with _internal_fill_value. See:

db_dtypes/pandas_backports.py

Lines changed: 1 addition & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,13 @@
1919
the versions in the later versions of pandas.
2020
"""
2121

22-
from typing import Any
23-
24-
import numpy
2522
import packaging.version
2623
import pandas
27-
from pandas.api.types import is_integer
2824
import pandas.compat.numpy.function
29-
import pandas.core.nanops
3025

3126
pandas_release = packaging.version.parse(pandas.__version__).release
3227

33-
# Create aliases for private methods in case they move in a future version.
28+
# # Create aliases for private methods in case they move in a future version.
3429
nanall = pandas.core.nanops.nanall
3530
nanany = pandas.core.nanops.nanany
3631
nanmax = pandas.core.nanops.nanmax
@@ -77,89 +72,3 @@ def import_default(module_name, force=False, default=None):
7772
class OpsMixin:
7873
def _cmp_method(self, other, op): # pragma: NO COVER
7974
return NotImplemented
80-
81-
82-
# TODO: use public API once NDArrayBackedExtensionArray is added to the
83-
# pandas.
84-
# See: https://github.com/pandas-dev/pandas/pull/45544
85-
# Also: Right now none of this is tested in test_pandas_backports.
86-
# Temporarily marking this as # pragma: NO COVER just to see how this
87-
# affects unittest coverage
88-
@import_default("pandas.core.arrays._mixins")
89-
class NDArrayBackedExtensionArray(
90-
pandas.core.arrays.base.ExtensionArray
91-
): # pragma: NO COVER
92-
def __init__(self, values, dtype):
93-
assert isinstance(values, numpy.ndarray)
94-
self._ndarray = values
95-
self._dtype = dtype
96-
97-
@classmethod
98-
def _from_backing_data(cls, data):
99-
return cls(data, data.dtype)
100-
101-
def __getitem__(self, index):
102-
value = self._ndarray[index]
103-
if is_integer(index):
104-
return self._box_func(value)
105-
return self.__class__(value, self._dtype)
106-
107-
def __setitem__(self, index, value):
108-
self._ndarray[index] = self._validate_setitem_value(value)
109-
110-
def __len__(self):
111-
return len(self._ndarray)
112-
113-
@property
114-
def shape(self):
115-
return self._ndarray.shape
116-
117-
@property
118-
def ndim(self) -> int:
119-
return self._ndarray.ndim
120-
121-
@property
122-
def size(self) -> int:
123-
return self._ndarray.size
124-
125-
@property
126-
def nbytes(self) -> int:
127-
return self._ndarray.nbytes
128-
129-
def copy(self):
130-
return self[:]
131-
132-
def repeat(self, n):
133-
return self.__class__(self._ndarray.repeat(n), self._dtype)
134-
135-
def take(
136-
self,
137-
indices,
138-
*,
139-
allow_fill: bool = False,
140-
fill_value: Any = None,
141-
axis: int = 0,
142-
):
143-
from pandas.core.algorithms import take
144-
145-
if allow_fill:
146-
fill_value = self._validate_scalar(fill_value)
147-
148-
new_data = take(
149-
self._ndarray,
150-
indices,
151-
allow_fill=allow_fill,
152-
fill_value=fill_value,
153-
axis=axis,
154-
)
155-
return self._from_backing_data(new_data)
156-
157-
@classmethod
158-
def _concat_same_type(cls, to_concat, axis=0):
159-
dtypes = {str(x.dtype) for x in to_concat}
160-
if len(dtypes) != 1:
161-
raise ValueError("to_concat must have the same dtype (tz)", dtypes)
162-
163-
new_values = [x._ndarray for x in to_concat]
164-
new_values = numpy.concatenate(new_values, axis=axis)
165-
return to_concat[0]._from_backing_data(new_values) # type: ignore[arg-type]

0 commit comments

Comments
 (0)