From a775f9cb40a6cd41d52c44261cbb1594705ffcee Mon Sep 17 00:00:00 2001 From: barnargh Date: Sat, 20 Apr 2024 20:19:38 -0500 Subject: [PATCH 1/2] fixed implicit conversion of 1-arrays inside data frames --- pandas/core/internals/managers.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index 8fda9cd23b508..d09e0fb74463a 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -2170,8 +2170,10 @@ def setitem_inplace(self, indexer, value) -> None: # Note: checking for ndarray instead of np.dtype means we exclude # 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, ...] From 1d04d6f959239e709404a4519518f651e9c32d49 Mon Sep 17 00:00:00 2001 From: barnargh Date: Sat, 20 Apr 2024 22:54:45 -0500 Subject: [PATCH 2/2] fixed issue #57944 --- pandas/io/parsers/readers.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pandas/io/parsers/readers.py b/pandas/io/parsers/readers.py index 70f9a68244164..1eb736880c369 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(