Skip to content

Commit bc50dc0

Browse files
committed
Move to internals
1 parent 7efafa6 commit bc50dc0

File tree

6 files changed

+75
-69
lines changed

6 files changed

+75
-69
lines changed

pandas/core/arrays/categorical.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
from pandas.core.dtypes.cast import (
1717
coerce_indexer_dtype, maybe_infer_to_datetimelike)
1818
from pandas.core.dtypes.common import (
19-
ensure_int64, ensure_object, ensure_platform_int, extract_array,
20-
is_categorical, is_categorical_dtype, is_datetime64_dtype, is_datetimelike,
21-
is_dict_like, is_dtype_equal, is_extension_array_dtype, is_float_dtype,
22-
is_integer_dtype, is_iterator, is_list_like, is_object_dtype, is_scalar,
23-
is_sequence, is_timedelta64_dtype)
19+
ensure_int64, ensure_object, ensure_platform_int, is_categorical,
20+
is_categorical_dtype, is_datetime64_dtype, is_datetimelike, is_dict_like,
21+
is_dtype_equal, is_extension_array_dtype, is_float_dtype, is_integer_dtype,
22+
is_iterator, is_list_like, is_object_dtype, is_scalar, is_sequence,
23+
is_timedelta64_dtype)
2424
from pandas.core.dtypes.dtypes import CategoricalDtype
2525
from pandas.core.dtypes.generic import (
2626
ABCCategoricalIndex, ABCIndexClass, ABCSeries)
@@ -2091,6 +2091,7 @@ def __setitem__(self, key, value):
20912091
If (one or more) Value is not in categories or if a assigned
20922092
`Categorical` does not have the same categories
20932093
"""
2094+
from pandas.core.internals.arrays import extract_array
20942095

20952096
value = extract_array(value, extract_numpy=True)
20962097

pandas/core/arrays/numpy_.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from pandas._libs import lib
66
from pandas.compat.numpy import function as nv
77

8-
from pandas.core.dtypes.common import extract_array
98
from pandas.core.dtypes.dtypes import ExtensionDtype
109
from pandas.core.dtypes.generic import ABCIndexClass, ABCSeries
1110
from pandas.core.dtypes.inference import is_list_like
@@ -210,6 +209,8 @@ def __getitem__(self, item):
210209
return result
211210

212211
def __setitem__(self, key, value):
212+
from pandas.core.internals.arrays import extract_array
213+
213214
value = extract_array(value, extract_numpy=True)
214215

215216
if not lib.is_scalar(key) and is_list_like(key):

pandas/core/dtypes/common.py

+2-53
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
registry)
1515
from pandas.core.dtypes.generic import (
1616
ABCCategorical, ABCCategoricalIndex, ABCDateOffset, ABCDatetimeIndex,
17-
ABCIndexClass, ABCPandasArray, ABCPeriodArray, ABCPeriodIndex, ABCSeries,
18-
ABCSparseArray, ABCSparseSeries)
17+
ABCIndexClass, ABCPeriodArray, ABCPeriodIndex, ABCSeries, ABCSparseArray,
18+
ABCSparseSeries)
1919
from pandas.core.dtypes.inference import ( # noqa:F401
2020
is_array_like, is_bool, is_complex, is_decimal, is_dict_like, is_file_like,
2121
is_float, is_hashable, is_integer, is_interval, is_iterator, is_list_like,
@@ -2016,54 +2016,3 @@ def pandas_dtype(dtype):
20162016
raise TypeError("dtype '{}' not understood".format(dtype))
20172017

20182018
return npdtype
2019-
2020-
2021-
def extract_array(obj, extract_numpy=False):
2022-
"""
2023-
Extract the ndarray or ExtensionArray from a Series or Index.
2024-
2025-
For all other types, `obj` is just returned as is.
2026-
2027-
Parameters
2028-
----------
2029-
obj : object
2030-
For Series / Index, the underlying ExtensionArray is unboxed.
2031-
For Numpy-backed ExtensionArrays, the ndarray is extracted.
2032-
2033-
extract_numpy : bool, default False
2034-
Whether to extract the ndarray from a PandasArray
2035-
2036-
Returns
2037-
-------
2038-
arr : object
2039-
2040-
Examples
2041-
--------
2042-
>>> extract_array(pd.Series(['a', 'b', 'c'], dtype='category'))
2043-
[a, b, c]
2044-
Categories (3, object): [a, b, c]
2045-
2046-
Other objects like lists, arrays, and DataFrames are just passed through.
2047-
2048-
>>> extract_array([1, 2, 3])
2049-
[1, 2, 3]
2050-
2051-
For an ndarray-backed Series / Index a PandasArray is returned.
2052-
2053-
>>> extract_array(pd.Series([1, 2, 3]))
2054-
<PandasArray>
2055-
[1, 2, 3]
2056-
Length: 3, dtype: int64
2057-
2058-
To extract all the way down to the ndarray, pass ``extract_numpy=True``.
2059-
2060-
>>> extract_array(pd.Series([1, 2, 3]), extract_numpy=True)
2061-
array([1, 2, 3])
2062-
"""
2063-
if isinstance(obj, (ABCIndexClass, ABCSeries)):
2064-
obj = obj.array
2065-
2066-
if extract_numpy and isinstance(obj, ABCPandasArray):
2067-
obj = obj._ndarray
2068-
2069-
return obj

pandas/core/internals/arrays.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""
2+
Methods for cleaning, validating, and unboxing arrays.
3+
"""
4+
from pandas.core.dtypes.generic import ABCIndexClass, ABCPandasArray, ABCSeries
5+
6+
7+
def extract_array(obj, extract_numpy=False):
8+
"""
9+
Extract the ndarray or ExtensionArray from a Series or Index.
10+
11+
For all other types, `obj` is just returned as is.
12+
13+
Parameters
14+
----------
15+
obj : object
16+
For Series / Index, the underlying ExtensionArray is unboxed.
17+
For Numpy-backed ExtensionArrays, the ndarray is extracted.
18+
19+
extract_numpy : bool, default False
20+
Whether to extract the ndarray from a PandasArray
21+
22+
Returns
23+
-------
24+
arr : object
25+
26+
Examples
27+
--------
28+
>>> extract_array(pd.Series(['a', 'b', 'c'], dtype='category'))
29+
[a, b, c]
30+
Categories (3, object): [a, b, c]
31+
32+
Other objects like lists, arrays, and DataFrames are just passed through.
33+
34+
>>> extract_array([1, 2, 3])
35+
[1, 2, 3]
36+
37+
For an ndarray-backed Series / Index a PandasArray is returned.
38+
39+
>>> extract_array(pd.Series([1, 2, 3]))
40+
<PandasArray>
41+
[1, 2, 3]
42+
Length: 3, dtype: int64
43+
44+
To extract all the way down to the ndarray, pass ``extract_numpy=True``.
45+
46+
>>> extract_array(pd.Series([1, 2, 3]), extract_numpy=True)
47+
array([1, 2, 3])
48+
"""
49+
if isinstance(obj, (ABCIndexClass, ABCSeries)):
50+
obj = obj.array
51+
52+
if extract_numpy and isinstance(obj, ABCPandasArray):
53+
obj = obj.to_numpy()
54+
55+
return obj

pandas/core/internals/blocks.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@
1818
infer_dtype_from_scalar, maybe_convert_objects, maybe_downcast_to_dtype,
1919
maybe_infer_dtype_type, maybe_promote, maybe_upcast, soft_convert_objects)
2020
from pandas.core.dtypes.common import (
21-
_NS_DTYPE, _TD_DTYPE, ensure_platform_int, extract_array, is_bool_dtype,
22-
is_categorical, is_categorical_dtype, is_datetime64_dtype,
23-
is_datetime64tz_dtype, is_dtype_equal, is_extension_array_dtype,
24-
is_extension_type, is_float_dtype, is_integer, is_integer_dtype,
25-
is_interval_dtype, is_list_like, is_numeric_v_string_like, is_object_dtype,
26-
is_period_dtype, is_re, is_re_compilable, is_sparse, is_timedelta64_dtype,
27-
pandas_dtype)
21+
_NS_DTYPE, _TD_DTYPE, ensure_platform_int, is_bool_dtype, is_categorical,
22+
is_categorical_dtype, is_datetime64_dtype, is_datetime64tz_dtype,
23+
is_dtype_equal, is_extension_array_dtype, is_extension_type,
24+
is_float_dtype, is_integer, is_integer_dtype, is_interval_dtype,
25+
is_list_like, is_numeric_v_string_like, is_object_dtype, is_period_dtype,
26+
is_re, is_re_compilable, is_sparse, is_timedelta64_dtype, pandas_dtype)
2827
import pandas.core.dtypes.concat as _concat
2928
from pandas.core.dtypes.dtypes import (
3029
CategoricalDtype, DatetimeTZDtype, ExtensionDtype, PandasExtensionDtype)
@@ -41,6 +40,7 @@
4140
from pandas.core.indexes.datetimes import DatetimeIndex
4241
from pandas.core.indexes.timedeltas import TimedeltaIndex
4342
from pandas.core.indexing import check_setitem_lengths
43+
from pandas.core.internals.arrays import extract_array
4444
import pandas.core.missing as missing
4545

4646
from pandas.io.formats.printing import pprint_thing

pandas/core/reshape/reshape.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111

1212
from pandas.core.dtypes.cast import maybe_promote
1313
from pandas.core.dtypes.common import (
14-
ensure_platform_int, extract_array, is_bool_dtype,
15-
is_extension_array_dtype, is_integer_dtype, is_list_like, is_object_dtype,
16-
needs_i8_conversion)
14+
ensure_platform_int, is_bool_dtype, is_extension_array_dtype,
15+
is_integer_dtype, is_list_like, is_object_dtype, needs_i8_conversion)
1716
from pandas.core.dtypes.missing import notna
1817

1918
from pandas import compat
@@ -22,6 +21,7 @@
2221
from pandas.core.arrays.categorical import _factorize_from_iterable
2322
from pandas.core.frame import DataFrame
2423
from pandas.core.index import Index, MultiIndex
24+
from pandas.core.internals.arrays import extract_array
2525
from pandas.core.series import Series
2626
from pandas.core.sorting import (
2727
compress_group_index, decons_obs_group_ids, get_compressed_ids,

0 commit comments

Comments
 (0)