Skip to content

Commit 5fa51d8

Browse files
committed
define pd_array in construction
1 parent ea19b7c commit 5fa51d8

13 files changed

+26
-27
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,
8887
ensure_wrapped_if_datetimelike,
8988
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 array
46+
from pandas.core.construction import pd_array as 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,
5655
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,
101100
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,
111110
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,
8079
ensure_wrapped_if_datetimelike,
8180
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 array(
77+
def pd_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 = array(subarr)
563+
subarr = pd_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,
2928
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 array as pd_array
517+
from pandas.core.construction import 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 array as pd_array
51+
from pandas.core.construction import pd_array
5252
from pandas.core.indexers import (
5353
check_array_indexer,
5454
is_exact_shape_match,

pandas/core/strings/accessor.py

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

30293027
regex = re.compile(pat, flags=flags)
30303028
groups_or_na = _groups_or_na_fun(regex)

scripts/tests/test_use_pd_array_in_core.py

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

33
from scripts.use_pd_array_in_core import use_pd_array
44

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"
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"
98
PATH = "t.py"
109

1110

1211
@pytest.mark.parametrize("content", [BAD_FILE_0, BAD_FILE_1])
1312
def test_inconsistent_usage(content, capsys):
1413
result_msg = (
15-
r"t.py:2:0: Don't use pd.array in core, import array as pd_array instead\n"
14+
"t.py:2:0: Don't use pd.array in core, "
15+
"instead use 'from pandas.core.construction import pd_array'\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-
@pytest.mark.parametrize("content", [GOOD_FILE_0, GOOD_FILE_1])
24-
def test_consistent_usage(content):
23+
def test_consistent_usage():
2524
# should not raise
26-
use_pd_array(content, PATH)
25+
use_pd_array(GOOD_FILE, PATH)

scripts/use_pd_array_in_core.py

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

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

2527

@@ -33,7 +35,7 @@ def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
3335
if (
3436
node.module is not None
3537
and node.module.startswith("pandas")
36-
and any(i.name == "array" and i.asname != "pd_array" for i in node.names)
38+
and any(i.name == "array" for i in node.names)
3739
):
3840
msg = ERROR_MESSAGE.format(
3941
path=self.path, lineno=node.lineno, col_offset=node.col_offset

0 commit comments

Comments
 (0)