Skip to content

Commit fcb8403

Browse files
bashtagejreback
authored andcommitted
ENH: Allow export of mixed columns to Stata strl (#23692)
Enable export of large columns to Stata strls when the column contains None as a null value closes #23633
1 parent 3edc18d commit fcb8403

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

doc/source/whatsnew/v0.24.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ Other Enhancements
239239
- :meth:`Timestamp.tz_localize`, :meth:`DatetimeIndex.tz_localize`, and :meth:`Series.tz_localize` have gained the ``nonexistent`` argument for alternative handling of nonexistent times. See :ref:`timeseries.timezone_nonexistent` (:issue:`8917`)
240240
- :meth:`read_excel()` now accepts ``usecols`` as a list of column names or callable (:issue:`18273`)
241241
- :meth:`MultiIndex.to_flat_index` has been added to flatten multiple levels into a single-level :class:`Index` object.
242+
- :meth:`DataFrame.to_stata` and :class:` pandas.io.stata.StataWriter117` can write mixed sting columns to Stata strl format (:issue:`23633`)
242243

243244
.. _whatsnew_0240.api_breaking:
244245

pandas/io/stata.py

+2
Original file line numberDiff line numberDiff line change
@@ -2558,6 +2558,8 @@ def generate_table(self):
25582558
for o, (idx, row) in enumerate(selected.iterrows()):
25592559
for j, (col, v) in enumerate(col_index):
25602560
val = row[col]
2561+
# Allow columns with mixed str and None (GH 23633)
2562+
val = '' if val is None else val
25612563
key = gso_table.get(val, None)
25622564
if key is None:
25632565
# Stata prefers human numbers

pandas/tests/io/test_stata.py

+17
Original file line numberDiff line numberDiff line change
@@ -1505,3 +1505,20 @@ def test_unicode_dta_118(self):
15051505
expected = pd.DataFrame(values, columns=columns)
15061506

15071507
tm.assert_frame_equal(unicode_df, expected)
1508+
1509+
def test_mixed_string_strl(self):
1510+
# GH 23633
1511+
output = [
1512+
{'mixed': 'string' * 500,
1513+
'number': 0},
1514+
{'mixed': None,
1515+
'number': 1}
1516+
]
1517+
1518+
output = pd.DataFrame(output)
1519+
with tm.ensure_clean() as path:
1520+
output.to_stata(path, write_index=False, version=117)
1521+
reread = read_stata(path)
1522+
expected = output.fillna('')
1523+
expected.number = expected.number.astype('int32')
1524+
tm.assert_frame_equal(reread, expected)

0 commit comments

Comments
 (0)