diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index 8fda9cd23b508..6b04f04262b13 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -2171,7 +2171,10 @@ def setitem_inplace(self, indexer, value) -> None: # dt64/td64, which do their own validation. value = np_can_hold_element(arr.dtype, value) - if isinstance(value, np.ndarray) and value.ndim == 1 and len(value) == 1: + # check if the dtype of the block is object + implicit_convert = arr.dtype != 'object' + if (isinstance(value, np.ndarray) and value.ndim == 1 and len(value) == 1 + and implicit_convert): # NumPy 1.25 deprecation: https://github.com/numpy/numpy/pull/10615 value = value[0, ...] diff --git a/pandas/io/parsers/readers.py b/pandas/io/parsers/readers.py index 70f9a68244164..e0060048dcae2 100644 --- a/pandas/io/parsers/readers.py +++ b/pandas/io/parsers/readers.py @@ -716,6 +716,19 @@ def read_csv( ) -> DataFrame | TextFileReader: ... +# a helper function for the read_csv(...) below). +# ensures that all keys in dtype are of type str. +# this allows for compatibility with the csv library +def parse_dtype(dtype) -> DtypeArg: + temp = {} + for key in dtype: + if isinstance(key, str): + temp[f"{key}"] = dtype[key] + else: + temp[key] = dtype[key] + return temp + + @Appender( _doc_read_csv_and_table.format( func_name="read_csv", @@ -790,6 +803,9 @@ def read_csv( storage_options: StorageOptions | None = None, dtype_backend: DtypeBackend | lib.NoDefault = lib.no_default, ) -> DataFrame | TextFileReader: + # ensures that all keys in dtype are a string for compatibility with csv + dtype = parse_dtype(dtype) + if keep_date_col is not lib.no_default: # GH#55569 warnings.warn( @@ -2026,4 +2042,4 @@ def _validate_skipfooter(kwds: dict[str, Any]) -> None: if kwds.get("iterator") or kwds.get("chunksize"): raise ValueError("'skipfooter' not supported for iteration") if kwds.get("nrows"): - raise ValueError("'skipfooter' not supported with 'nrows'") + raise ValueError("'skipfooter' not supported with 'nrows'") \ No newline at end of file