mirror of
https://github.com/apache/arrow.git
synced 2026-10-29 17:15:39 +08:00
Pass through preserve_index to RecordBatch.from_pandas in serialize_pandas
Change-Id: I21268556e18b739db3de8601cce83388227dec0d
This commit is contained in:
1 parent
a0555c04dd
commit
04dc01713d
3 files changed
+17
-4
No files matched your search
@@ -142,7 +142,7 @@ def open_file(source, footer_offset=None):
|
||||
return RecordBatchFileReader(source, footer_offset=footer_offset)
|
||||
|
||||
|
||||
def serialize_pandas(df, nthreads=None):
|
||||
def serialize_pandas(df, nthreads=None, preserve_index=True):
|
||||
"""Serialize a pandas DataFrame into a buffer protocol compatible object.
|
||||
|
||||
Parameters
|
||||
@@ -150,13 +150,17 @@ def serialize_pandas(df, nthreads=None):
|
||||
df : pandas.DataFrame
|
||||
nthreads : int, default None
|
||||
Number of threads to use for conversion to Arrow, default all CPUs
|
||||
preserve_index : boolean, default True
|
||||
If True, preserve the pandas index data, otherwise the result will have
|
||||
a default RangeIndex
|
||||
|
||||
Returns
|
||||
-------
|
||||
buf : buffer
|
||||
An object compatible with the buffer protocol
|
||||
"""
|
||||
batch = pa.RecordBatch.from_pandas(df, nthreads=nthreads)
|
||||
batch = pa.RecordBatch.from_pandas(df, nthreads=nthreads,
|
||||
preserve_index=preserve_index)
|
||||
sink = pa.BufferOutputStream()
|
||||
writer = pa.RecordBatchStreamWriter(sink, batch.schema)
|
||||
writer.write_batch(batch)
|
||||
|
||||
@@ -485,8 +485,8 @@ def table_to_blockmanager(options, table, memory_pool, nthreads=1):
|
||||
)
|
||||
]
|
||||
new_levels = [
|
||||
level if level.dtype == dtype else level.astype(dtype)
|
||||
for level, dtype in levels_dtypes
|
||||
_level if _level.dtype == _dtype else _level.astype(_dtype)
|
||||
for _level, _dtype in levels_dtypes
|
||||
]
|
||||
columns = pd.MultiIndex(
|
||||
levels=new_levels,
|
||||
|
||||
@@ -416,6 +416,15 @@ def test_pandas_serialize_round_trip_not_string_columns():
|
||||
assert_frame_equal(result, df)
|
||||
|
||||
|
||||
def test_serialize_pandas_no_preserve_index():
|
||||
df = pd.DataFrame({'a': [1, 2, 3]}, index=[1, 2, 3])
|
||||
expected = pd.DataFrame({'a': [1, 2, 3]})
|
||||
|
||||
buf = pa.serialize_pandas(df, preserve_index=False)
|
||||
result = pa.deserialize_pandas(buf)
|
||||
assert_frame_equal(result, expected)
|
||||
|
||||
|
||||
def test_schema_batch_serialize_methods():
|
||||
nrows = 5
|
||||
df = pd.DataFrame({
|
||||
|
||||
Reference in new issue
Block a user