Skip to content

Commit bec9a10

Browse files
committed
import array as pd_array
1 parent 5fa51d8 commit bec9a10

13 files changed

+27
-26
lines changed

pandas/core/algorithms.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@
8484

8585
from pandas.core.array_algos.take import take_nd
8686
from pandas.core.construction import (
87+
array as pd_array,
8788
ensure_wrapped_if_datetimelike,
8889
extract_array,
89-
pd_array,
9090
)
9191
from pandas.core.indexers import validate_indices
9292

pandas/core/api.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
UInt64Dtype,
4444
)
4545
from pandas.core.arrays.string_ import StringDtype
46-
from pandas.core.construction import pd_array as array
46+
from pandas.core.construction import array
4747
from pandas.core.flags import Flags
4848
from pandas.core.groupby import (
4949
Grouper,

pandas/core/apply.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@
5252
)
5353
import pandas.core.common as com
5454
from pandas.core.construction import (
55+
array as pd_array,
5556
create_series_with_explicit_dtype,
56-
pd_array,
5757
)
5858

5959
if TYPE_CHECKING:

pandas/core/arrays/categorical.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@
9797
)
9898
import pandas.core.common as com
9999
from pandas.core.construction import (
100+
array as pd_array,
100101
extract_array,
101-
pd_array,
102102
sanitize_array,
103103
)
104104
from pandas.core.indexers import deprecate_ndim_indexing

pandas/core/arrays/datetimelike.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@
107107
)
108108
import pandas.core.common as com
109109
from pandas.core.construction import (
110+
array as pd_array,
110111
extract_array,
111-
pd_array,
112112
)
113113
from pandas.core.indexers import (
114114
check_array_indexer,

pandas/core/arrays/interval.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@
7676
from pandas.core.arrays.categorical import Categorical
7777
import pandas.core.common as com
7878
from pandas.core.construction import (
79+
array as pd_array,
7980
ensure_wrapped_if_datetimelike,
8081
extract_array,
81-
pd_array,
8282
)
8383
from pandas.core.indexers import check_array_indexer
8484
from pandas.core.indexes.base import ensure_index

pandas/core/construction.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
)
7575

7676

77-
def pd_array(
77+
def array(
7878
data: Union[Sequence[object], AnyArrayLike],
7979
dtype: Optional[Dtype] = None,
8080
copy: bool = True,
@@ -560,7 +560,7 @@ def sanitize_array(
560560
if is_object_dtype(subarr.dtype) and not is_object_or_str_dtype:
561561
inferred = lib.infer_dtype(subarr, skipna=False)
562562
if inferred in {"interval", "period"}:
563-
subarr = pd_array(subarr)
563+
subarr = array(subarr)
564564
subarr = extract_array(subarr, extract_numpy=True)
565565

566566
return subarr

pandas/core/dtypes/concat.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
from pandas.core.arrays import ExtensionArray
2626
from pandas.core.arrays.sparse import SparseArray
2727
from pandas.core.construction import (
28+
array as pd_array,
2829
ensure_wrapped_if_datetimelike,
29-
pd_array,
3030
)
3131

3232

pandas/core/indexers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ def check_array_indexer(array: AnyArrayLike, indexer: Any) -> Any:
514514
...
515515
IndexError: arrays used as indices must be of integer or boolean type
516516
"""
517-
from pandas.core.construction import pd_array
517+
from pandas.core.construction import array as pd_array
518518

519519
# whatever is not an array-like is returned as-is (possible valid array
520520
# indexers that are not array-like: integer, slice, Ellipsis, None)

pandas/core/indexing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
)
4949

5050
import pandas.core.common as com
51-
from pandas.core.construction import pd_array
51+
from pandas.core.construction import array as pd_array
5252
from pandas.core.indexers import (
5353
check_array_indexer,
5454
is_exact_shape_match,

pandas/core/strings/accessor.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -3021,8 +3021,10 @@ def _str_extract_noexpand(arr, pat, flags=0):
30213021
Index.
30223022
30233023
"""
3024-
from pandas import DataFrame
3025-
from pandas.core.construction import pd_array
3024+
from pandas import (
3025+
DataFrame,
3026+
array as pd_array,
3027+
)
30263028

30273029
regex = re.compile(pat, flags=flags)
30283030
groups_or_na = _groups_or_na_fun(regex)

scripts/tests/test_use_pd_array_in_core.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,25 @@
22

33
from scripts.use_pd_array_in_core import use_pd_array
44

5-
BAD_FILE_0 = "\nfrom pandas import array as pd_array"
6-
BAD_FILE_1 = "import pandas as pd\npd.array"
7-
GOOD_FILE = "from pandas.core.construction import pd_array"
5+
BAD_FILE_0 = "import pandas as pd\npd.array"
6+
BAD_FILE_1 = "\nfrom pandas import array"
7+
GOOD_FILE_0 = "from pandas import array as pd_array"
8+
GOOD_FILE_1 = "from pandas.core.construction import array as pd_array"
89
PATH = "t.py"
910

1011

1112
@pytest.mark.parametrize("content", [BAD_FILE_0, BAD_FILE_1])
1213
def test_inconsistent_usage(content, capsys):
1314
result_msg = (
14-
"t.py:2:0: Don't use pd.array in core, "
15-
"instead use 'from pandas.core.construction import pd_array'\n"
15+
r"t.py:2:0: Don't use pd.array in core, import array as pd_array instead\n"
1616
)
1717
with pytest.raises(SystemExit):
1818
use_pd_array(content, PATH)
1919
expected_msg, _ = capsys.readouterr()
2020
assert result_msg == expected_msg
2121

2222

23-
def test_consistent_usage():
23+
@pytest.mark.parametrize("content", [GOOD_FILE_0, GOOD_FILE_1])
24+
def test_consistent_usage(content):
2425
# should not raise
25-
use_pd_array(GOOD_FILE, PATH)
26+
use_pd_array(content, PATH)

scripts/use_pd_array_in_core.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
"""
2-
Check files use pd_array from pandas.core.construction rather than pd.array.
2+
Check that pandas/core imports pandas.array as pd_array.
33
4-
This is only run within pandas/core and makes it easier to grep for usage of
5-
pandas array.
4+
This makes it easier to grep for usage of pandas array.
65
76
This is meant to be run as a pre-commit hook - to run it manually, you can do:
87
@@ -20,8 +19,7 @@
2019

2120
ERROR_MESSAGE = (
2221
"{path}:{lineno}:{col_offset}: "
23-
"Don't use pd.array in core, instead use "
24-
"'from pandas.core.construction import pd_array'\n"
22+
"Don't use pd.array in core, import array as pd_array instead\n"
2523
)
2624

2725

@@ -35,7 +33,7 @@ def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
3533
if (
3634
node.module is not None
3735
and node.module.startswith("pandas")
38-
and any(i.name == "array" for i in node.names)
36+
and any(i.name == "array" and i.asname != "pd_array" for i in node.names)
3937
):
4038
msg = ERROR_MESSAGE.format(
4139
path=self.path, lineno=node.lineno, col_offset=node.col_offset

0 commit comments

Comments
 (0)