1515from decimal import Decimal
1616
1717import numpy as np
18- import pandas as pd
18+
19+ try :
20+ import pandas as pd
21+ except ImportError :
22+ pd = None
1923
2024try :
2125 import polars as pl
@@ -170,6 +174,9 @@ def _arrow_to_pandas(arrow_table):
170174 See https://arrow.apache.org/docs/python/pandas.html#reducing-memory-use-in-table-to-pandas
171175 for details.
172176 """
177+ if pd is None :
178+ msg = "pandas is not installed. Try pip install pandas."
179+ raise ValueError (msg )
173180 return arrow_table .to_pandas (split_blocks = True , self_destruct = True )
174181
175182
@@ -238,10 +245,10 @@ def _arrow_to_numpy(arrow_table, schema=None):
238245
239246 for fname in schema :
240247 dtype = get_numpy_type (schema [fname ])
248+ container [fname ] = arrow_table [fname ].to_numpy ()
241249 if dtype == np .str_ :
242- container [fname ] = arrow_table [fname ].to_pandas ().to_numpy (dtype = dtype )
243- else :
244- container [fname ] = arrow_table [fname ].to_numpy ()
250+ container [fname ] = container [fname ].astype (np .str_ )
251+
245252 return container
246253
247254
@@ -427,7 +434,7 @@ def _tabular_generator(tabular, *, exclude_none=False):
427434 yield {k : v for k , v in row .items () if v is not None }
428435 else :
429436 yield row
430- elif isinstance (tabular , pd .DataFrame ):
437+ elif pd is not None and isinstance (tabular , pd .DataFrame ):
431438 for row in tabular .to_dict ("records" ):
432439 if exclude_none :
433440 yield {k : v for k , v in row .items () if not np .isnan (v )}
@@ -498,7 +505,7 @@ def write(collection, tabular, *, exclude_none: bool = False):
498505 cols = [tabular .column (i ).cast (new_types [i ]) for i in range (tabular .num_columns )]
499506 tabular = Table .from_arrays (cols , names = tabular .column_names )
500507 _validate_schema (tabular .schema .types )
501- elif isinstance (tabular , pd .DataFrame ):
508+ elif pd is not None and isinstance (tabular , pd .DataFrame ):
502509 _validate_schema (ArrowSchema .from_pandas (tabular ).types )
503510 elif pl is not None and isinstance (tabular , pl .DataFrame ):
504511 tabular = tabular .to_arrow () # zero-copy in most cases and done in tabular_gen anyway
@@ -523,7 +530,10 @@ def write(collection, tabular, *, exclude_none: bool = False):
523530
524531 # Add handling for special case types.
525532 codec_options = collection .codec_options
526- type_registry = TypeRegistry ([_PandasNACodec (), _DecimalCodec ()])
533+ if pd is not None :
534+ type_registry = TypeRegistry ([_PandasNACodec (), _DecimalCodec ()])
535+ else :
536+ type_registry = TypeRegistry ([_DecimalCodec ()])
527537 codec_options = codec_options .with_options (type_registry = type_registry )
528538
529539 while cur_offset < tab_size :
0 commit comments