Skip to content

TYP: writers.pyi #40832

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions pandas/_libs/writers.pyi
Original file line number Diff line number Diff line change
@@ -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: ...
6 changes: 2 additions & 4 deletions pandas/_libs/writers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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
10 changes: 9 additions & 1 deletion pandas/io/formats/csvs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
2 changes: 1 addition & 1 deletion pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow i am shocked this didn't break anything (meaning how was this working before)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the object returned here was the same as the input

libwriters.string_array_replace_from_nan_rep(data, nan_rep)
return data.reshape(shape)


Expand Down