Skip to content

Commit 54e00d5

Browse files
kinowBruno P. Kinoshita
authored and
Bruno P. Kinoshita
committed
Fix pandas-dev#25099 set na_rep values before converting to string to prevent data truncation
1 parent fb11912 commit 54e00d5

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

pandas/core/internals/blocks.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,10 @@ def to_native_types(self, slicer=None, na_rep='nan', quoting=None,
726726
mask = isna(values)
727727

728728
if not self.is_object and not quoting:
729-
values = values.astype(str)
729+
if na_rep and isinstance(na_rep, str):
730+
values = values.astype("<U{length}".format(length=len(na_rep)))
731+
else:
732+
values = values.astype(str)
730733
else:
731734
values = np.array(values, dtype='object')
732735

pandas/tests/io/formats/test_to_csv.py

+16
Original file line numberDiff line numberDiff line change
@@ -561,3 +561,19 @@ def test_to_csv_compression(self, compression_only,
561561
result = pd.read_csv(path, index_col=0,
562562
compression=read_compression)
563563
tm.assert_frame_equal(result, df)
564+
565+
def test_to_csv_na_rep_long_string(self, capsys):
566+
# see gh-25099
567+
df = pd.DataFrame({"c": [float('nan')] * 3})
568+
df = df.astype("Int64")
569+
expected_rows = ['c',
570+
'mynull',
571+
'mynull',
572+
'mynull']
573+
expected_ascii = tm.convert_rows_list_to_csv_str(expected_rows)
574+
575+
df.to_csv(sys.stdout, index=False, na_rep='mynull', encoding='ascii')
576+
captured = capsys.readouterr()
577+
578+
assert captured.out == expected_ascii
579+
assert not sys.stdout.closed

0 commit comments

Comments
 (0)