diff --git a/pandas/_libs/writers.pyi b/pandas/_libs/writers.pyi new file mode 100644 index 0000000000000..67f6059c2a825 --- /dev/null +++ b/pandas/_libs/writers.pyi @@ -0,0 +1,24 @@ +import numpy as np + +# TODO: can make this more specific +def write_csv_rows( + data: list, + data_index: np.ndarray, + nlevels: int, + cols: np.ndarray, + writer: object, # _csv.writer +) -> None: ... + +def convert_json_to_lines(arr: str) -> str: ... + +def max_len_string_array( + arr: np.ndarray, # pandas_string[:] +) -> int: ... + +def word_len(val: object) -> int: ... + +def string_array_replace_from_nan_rep( + arr: np.ndarray, # np.ndarray[object, ndim=1] + nan_rep: object, + replace: object = ..., +) -> None: ... diff --git a/pandas/_libs/writers.pyx b/pandas/_libs/writers.pyx index 9fbeb67aa35e9..79f551c9ebf6f 100644 --- a/pandas/_libs/writers.pyx +++ b/pandas/_libs/writers.pyx @@ -23,7 +23,7 @@ def write_csv_rows( Py_ssize_t nlevels, ndarray cols, object writer -): +) -> None: """ Write the given data to the writer object, pre-allocating where possible for performance improvements. @@ -162,7 +162,7 @@ def string_array_replace_from_nan_rep( ndarray[object, ndim=1] arr, object nan_rep, object replace=np.nan -): +) -> None: """ Replace the values in the array with 'replacement' if they are 'nan_rep'. Return the same array. @@ -173,5 +173,3 @@ def string_array_replace_from_nan_rep( for i in range(length): if arr[i] == nan_rep: arr[i] = replace - - return arr diff --git a/pandas/io/formats/csvs.py b/pandas/io/formats/csvs.py index 2d4d67a533874..8ba38a44ecd2e 100644 --- a/pandas/io/formats/csvs.py +++ b/pandas/io/formats/csvs.py @@ -308,4 +308,12 @@ def _save_chunk(self, start_i: int, end_i: int) -> None: data = [res.iget_values(i) for i in range(len(res.items))] ix = self.data_index[slicer]._format_native_types(**self._number_format) - libwriters.write_csv_rows(data, ix, self.nlevels, self.cols, self.writer) + # error: Argument 4 to "write_csv_rows" has incompatible type + # "Sequence[Hashable]"; expected "ndarray" + libwriters.write_csv_rows( + data, + ix, + self.nlevels, + self.cols, # type: ignore[arg-type] + self.writer, + ) diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index a718ce1b6b9ec..10d55053d5bc6 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -5066,7 +5066,7 @@ def _unconvert_string_array( if nan_rep is None: nan_rep = "nan" - data = libwriters.string_array_replace_from_nan_rep(data, nan_rep) + libwriters.string_array_replace_from_nan_rep(data, nan_rep) return data.reshape(shape)