Skip to content

Commit 440a7de

Browse files
authored
TYP: internals.construction (#40154)
1 parent 8c1a507 commit 440a7de

File tree

3 files changed

+24
-25
lines changed

3 files changed

+24
-25
lines changed

pandas/core/dtypes/cast.py

+3
Original file line numberDiff line numberDiff line change
@@ -1620,6 +1620,7 @@ def maybe_cast_to_datetime(
16201620
dta = dta.tz_localize(None)
16211621
value = dta
16221622
elif is_datetime64tz:
1623+
dtype = cast(DatetimeTZDtype, dtype)
16231624
# The string check can be removed once issue #13712
16241625
# is solved. String data that is passed with a
16251626
# datetime64tz is assumed to be naive which should
@@ -1700,6 +1701,8 @@ def ensure_nanosecond_dtype(dtype: DtypeObj) -> DtypeObj:
17001701
dtype('<M8[ns]')
17011702
17021703
>>> ensure_nanosecond_dtype(np.dtype("m8[ps]"))
1704+
Traceback (most recent call last):
1705+
...
17031706
TypeError: cannot convert timedeltalike to dtype [timedelta64[ps]]
17041707
"""
17051708
msg = (

pandas/core/frame.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -1939,12 +1939,11 @@ def from_records(
19391939
arr_columns_list.append(k)
19401940
arrays.append(v)
19411941

1942-
arrays, arr_columns = reorder_arrays(arrays, arr_columns_list, columns)
1942+
arr_columns = Index(arr_columns_list)
1943+
arrays, arr_columns = reorder_arrays(arrays, arr_columns, columns)
19431944

19441945
elif isinstance(data, (np.ndarray, DataFrame)):
19451946
arrays, columns = to_arrays(data, columns)
1946-
if columns is not None:
1947-
columns = ensure_index(columns)
19481947
arr_columns = columns
19491948
else:
19501949
arrays, arr_columns = to_arrays(data, columns)
@@ -1954,9 +1953,7 @@ def from_records(
19541953
arrays[i] = lib.maybe_convert_objects(arr, try_float=True)
19551954

19561955
arr_columns = ensure_index(arr_columns)
1957-
if columns is not None:
1958-
columns = ensure_index(columns)
1959-
else:
1956+
if columns is None:
19601957
columns = arr_columns
19611958

19621959
if exclude is None:

pandas/core/internals/construction.py

+18-19
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
if TYPE_CHECKING:
8080
from numpy.ma.mrecords import MaskedRecords
8181

82+
8283
# ---------------------------------------------------------------------
8384
# BlockManager Interface
8485

@@ -91,7 +92,7 @@ def arrays_to_mgr(
9192
dtype: Optional[DtypeObj] = None,
9293
verify_integrity: bool = True,
9394
typ: Optional[str] = None,
94-
):
95+
) -> Manager:
9596
"""
9697
Segregate Series based on type and coerce into matrices.
9798
@@ -109,11 +110,11 @@ def arrays_to_mgr(
109110
# don't force copy because getting jammed in an ndarray anyway
110111
arrays = _homogenize(arrays, index, dtype)
111112

112-
columns = ensure_index(columns)
113113
else:
114-
columns = ensure_index(columns)
115114
index = ensure_index(index)
116115

116+
columns = ensure_index(columns)
117+
117118
# from BlockManager perspective
118119
axes = [columns, index]
119120

@@ -140,9 +141,8 @@ def rec_array_to_mgr(
140141
fdata = ma.getdata(data)
141142
if index is None:
142143
index = _get_names_from_index(fdata)
143-
if index is None:
144-
index = ibase.default_index(len(data))
145-
index = ensure_index(index)
144+
else:
145+
index = ensure_index(index)
146146

147147
if columns is not None:
148148
columns = ensure_index(columns)
@@ -215,14 +215,14 @@ def mgr_to_mgr(mgr, typ: str):
215215

216216
def ndarray_to_mgr(
217217
values, index, columns, dtype: Optional[DtypeObj], copy: bool, typ: str
218-
):
218+
) -> Manager:
219219
# used in DataFrame.__init__
220-
# input must be a ndarray, list, Series, index
220+
# input must be a ndarray, list, Series, Index, ExtensionArray
221221

222222
if isinstance(values, ABCSeries):
223223
if columns is None:
224224
if values.name is not None:
225-
columns = [values.name]
225+
columns = Index([values.name])
226226
if index is None:
227227
index = values.index
228228
else:
@@ -309,7 +309,9 @@ def ndarray_to_mgr(
309309
return create_block_manager_from_blocks(block_values, [columns, index])
310310

311311

312-
def dict_to_mgr(data: Dict, index, columns, dtype: Optional[DtypeObj], typ: str):
312+
def dict_to_mgr(
313+
data: Dict, index, columns, dtype: Optional[DtypeObj], typ: str
314+
) -> Manager:
313315
"""
314316
Segregate Series based on type and coerce into matrices.
315317
Needs to handle a lot of exceptional cases.
@@ -531,21 +533,18 @@ def extract_index(data) -> Index:
531533
return ensure_index(index)
532534

533535

534-
def reorder_arrays(arrays, arr_columns, columns):
536+
def reorder_arrays(
537+
arrays: List[ArrayLike], arr_columns: Index, columns: Optional[Index]
538+
) -> Tuple[List[ArrayLike], Index]:
535539
# reorder according to the columns
536-
if (
537-
columns is not None
538-
and len(columns)
539-
and arr_columns is not None
540-
and len(arr_columns)
541-
):
540+
if columns is not None and len(columns) and len(arr_columns):
542541
indexer = ensure_index(arr_columns).get_indexer(columns)
543542
arr_columns = ensure_index([arr_columns[i] for i in indexer])
544543
arrays = [arrays[i] for i in indexer]
545544
return arrays, arr_columns
546545

547546

548-
def _get_names_from_index(data):
547+
def _get_names_from_index(data) -> Index:
549548
has_some_name = any(getattr(s, "name", None) is not None for s in data)
550549
if not has_some_name:
551550
return ibase.default_index(len(data))
@@ -560,7 +559,7 @@ def _get_names_from_index(data):
560559
index[i] = f"Unnamed {count}"
561560
count += 1
562561

563-
return index
562+
return Index(index)
564563

565564

566565
def _get_axes(

0 commit comments

Comments
 (0)