Skip to content

Commit 388cd59

Browse files
kinowBruno P. Kinoshita
authored and
Bruno P. Kinoshita
committed
Fix #25099 set na_rep values before converting to string to prevent data truncation
1 parent d3c9d6e commit 388cd59

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

pandas/core/internals/blocks.py

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

741741
if not self.is_object and not quoting:
742-
values = values.astype(str)
742+
if na_rep and isinstance(na_rep, str):
743+
values = values.astype("<U{length}".format(length=len(na_rep)))
744+
else:
745+
values = values.astype(str)
743746
else:
744747
values = np.array(values, dtype='object')
745748

pandas/tests/io/formats/test_to_csv.py

+19
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
from pandas import DataFrame, compat
1111
from pandas.util import testing as tm
1212

13+
from io import StringIO
14+
1315

1416
class TestToCSV(object):
1517

@@ -561,3 +563,20 @@ def test_to_csv_compression(self, compression_only,
561563
result = pd.read_csv(path, index_col=0,
562564
compression=read_compression)
563565
tm.assert_frame_equal(result, df)
566+
567+
def test_to_csv_na_rep_long_string(self, capsys):
568+
# see gh-25099
569+
df = pd.DataFrame({"c": [float('nan')] * 3})
570+
df = df.astype("Int64")
571+
expected_rows = ['c',
572+
'mynull',
573+
'mynull',
574+
'mynull']
575+
expected = tm.convert_rows_list_to_csv_str(expected_rows)
576+
577+
csv_output = StringIO()
578+
df.to_csv(csv_output, index=False, na_rep='mynull', encoding='ascii')
579+
result = csv_output.getvalue()
580+
581+
assert result == expected
582+

0 commit comments

Comments
 (0)