diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 178e6ce9b02eb..9f111282473c2 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -1620,6 +1620,7 @@ def maybe_cast_to_datetime( dta = dta.tz_localize(None) value = dta elif is_datetime64tz: + dtype = cast(DatetimeTZDtype, dtype) # The string check can be removed once issue #13712 # is solved. String data that is passed with a # datetime64tz is assumed to be naive which should @@ -1700,6 +1701,8 @@ def ensure_nanosecond_dtype(dtype: DtypeObj) -> DtypeObj: dtype('>> ensure_nanosecond_dtype(np.dtype("m8[ps]")) + Traceback (most recent call last): + ... TypeError: cannot convert timedeltalike to dtype [timedelta64[ps]] """ msg = ( diff --git a/pandas/core/frame.py b/pandas/core/frame.py index f0f8d813bba96..dcd6ef77238f9 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1939,12 +1939,11 @@ def from_records( arr_columns_list.append(k) arrays.append(v) - arrays, arr_columns = reorder_arrays(arrays, arr_columns_list, columns) + arr_columns = Index(arr_columns_list) + arrays, arr_columns = reorder_arrays(arrays, arr_columns, columns) elif isinstance(data, (np.ndarray, DataFrame)): arrays, columns = to_arrays(data, columns) - if columns is not None: - columns = ensure_index(columns) arr_columns = columns else: arrays, arr_columns = to_arrays(data, columns) @@ -1954,9 +1953,7 @@ def from_records( arrays[i] = lib.maybe_convert_objects(arr, try_float=True) arr_columns = ensure_index(arr_columns) - if columns is not None: - columns = ensure_index(columns) - else: + if columns is None: columns = arr_columns if exclude is None: diff --git a/pandas/core/internals/construction.py b/pandas/core/internals/construction.py index d49114c0da719..9a7ae39b9f8eb 100644 --- a/pandas/core/internals/construction.py +++ b/pandas/core/internals/construction.py @@ -79,6 +79,7 @@ if TYPE_CHECKING: from numpy.ma.mrecords import MaskedRecords + # --------------------------------------------------------------------- # BlockManager Interface @@ -91,7 +92,7 @@ def arrays_to_mgr( dtype: Optional[DtypeObj] = None, verify_integrity: bool = True, typ: Optional[str] = None, -): +) -> Manager: """ Segregate Series based on type and coerce into matrices. @@ -109,11 +110,11 @@ def arrays_to_mgr( # don't force copy because getting jammed in an ndarray anyway arrays = _homogenize(arrays, index, dtype) - columns = ensure_index(columns) else: - columns = ensure_index(columns) index = ensure_index(index) + columns = ensure_index(columns) + # from BlockManager perspective axes = [columns, index] @@ -140,9 +141,8 @@ def rec_array_to_mgr( fdata = ma.getdata(data) if index is None: index = _get_names_from_index(fdata) - if index is None: - index = ibase.default_index(len(data)) - index = ensure_index(index) + else: + index = ensure_index(index) if columns is not None: columns = ensure_index(columns) @@ -215,14 +215,14 @@ def mgr_to_mgr(mgr, typ: str): def ndarray_to_mgr( values, index, columns, dtype: Optional[DtypeObj], copy: bool, typ: str -): +) -> Manager: # used in DataFrame.__init__ - # input must be a ndarray, list, Series, index + # input must be a ndarray, list, Series, Index, ExtensionArray if isinstance(values, ABCSeries): if columns is None: if values.name is not None: - columns = [values.name] + columns = Index([values.name]) if index is None: index = values.index else: @@ -309,7 +309,9 @@ def ndarray_to_mgr( return create_block_manager_from_blocks(block_values, [columns, index]) -def dict_to_mgr(data: Dict, index, columns, dtype: Optional[DtypeObj], typ: str): +def dict_to_mgr( + data: Dict, index, columns, dtype: Optional[DtypeObj], typ: str +) -> Manager: """ Segregate Series based on type and coerce into matrices. Needs to handle a lot of exceptional cases. @@ -531,21 +533,18 @@ def extract_index(data) -> Index: return ensure_index(index) -def reorder_arrays(arrays, arr_columns, columns): +def reorder_arrays( + arrays: List[ArrayLike], arr_columns: Index, columns: Optional[Index] +) -> Tuple[List[ArrayLike], Index]: # reorder according to the columns - if ( - columns is not None - and len(columns) - and arr_columns is not None - and len(arr_columns) - ): + if columns is not None and len(columns) and len(arr_columns): indexer = ensure_index(arr_columns).get_indexer(columns) arr_columns = ensure_index([arr_columns[i] for i in indexer]) arrays = [arrays[i] for i in indexer] return arrays, arr_columns -def _get_names_from_index(data): +def _get_names_from_index(data) -> Index: has_some_name = any(getattr(s, "name", None) is not None for s in data) if not has_some_name: return ibase.default_index(len(data)) @@ -560,7 +559,7 @@ def _get_names_from_index(data): index[i] = f"Unnamed {count}" count += 1 - return index + return Index(index) def _get_axes(