Skip to content

Commit 220c3fc

Browse files
committed
GH-34577 [Python] Expose eol and null_string csv WriteOptions
1 parent 697f501 commit 220c3fc

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

python/pyarrow/_csv.pyx

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1355,6 +1355,10 @@ cdef class WriteOptions(_Weakrefable):
13551355
CSV data
13561356
delimiter : 1-character string, optional (default ",")
13571357
The character delimiting individual cells in the CSV data.
1358+
eol : str, optional (default "\\n")
1359+
The end of line character to use for ending rows
1360+
null_string : str, optional (default "")
1361+
The string to write for null values. Quotes are not allowed in this string.
13581362
quoting_style : str, optional (default "needed")
13591363
Whether to quote values, and if so, which quoting style to use.
13601364
The following values are accepted:
@@ -1370,14 +1374,18 @@ cdef class WriteOptions(_Weakrefable):
13701374
__slots__ = ()
13711375

13721376
def __init__(self, *, include_header=None, batch_size=None,
1373-
delimiter=None, quoting_style=None):
1377+
delimiter=None, eol=None, null_string=None, quoting_style=None):
13741378
self.options.reset(new CCSVWriteOptions(CCSVWriteOptions.Defaults()))
13751379
if include_header is not None:
13761380
self.include_header = include_header
13771381
if batch_size is not None:
13781382
self.batch_size = batch_size
13791383
if delimiter is not None:
13801384
self.delimiter = delimiter
1385+
if eol is not None:
1386+
self.eol = eol
1387+
if null_string is not None:
1388+
self.null_string = null_string
13811389
if quoting_style is not None:
13821390
self.quoting_style = quoting_style
13831391

@@ -1415,6 +1423,28 @@ cdef class WriteOptions(_Weakrefable):
14151423
def delimiter(self, value):
14161424
deref(self.options).delimiter = _single_char(value)
14171425

1426+
@property
1427+
def eol(self):
1428+
"""
1429+
The end of line character to use for ending rows
1430+
"""
1431+
return frombytes(deref(self.options).eol)
1432+
1433+
@eol.setter
1434+
def eol(self, value):
1435+
deref(self.options).eol = tobytes(value)
1436+
1437+
@property
1438+
def null_string(self):
1439+
"""
1440+
The string to write for null values. Quotes are not allowed in this string.
1441+
"""
1442+
return frombytes(deref(self.options).null_string)
1443+
1444+
@null_string.setter
1445+
def null_string(self, value):
1446+
deref(self.options).null_string = tobytes(value)
1447+
14181448
@property
14191449
def quoting_style(self):
14201450
"""

python/pyarrow/includes/libarrow.pxd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2147,6 +2147,8 @@ cdef extern from "arrow/csv/api.h" namespace "arrow::csv" nogil:
21472147
int32_t batch_size
21482148
unsigned char delimiter
21492149
CQuotingStyle quoting_style
2150+
c_string eol
2151+
c_string null_string
21502152
CIOContext io_context
21512153

21522154
CCSVWriteOptions()

python/pyarrow/tests/test_csv.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ def test_write_options():
361361

362362
check_options_class(
363363
cls, include_header=[True, False], delimiter=[',', '\t', '|'],
364+
eol=['\n', '\r\n'], null_string=['', 'NA'],
364365
quoting_style=['needed', 'none', 'all_valid'])
365366

366367
assert opts.batch_size > 0

0 commit comments

Comments
 (0)