Skip to content

Commit cf0cdf0

Browse files
authored
ARROW-126 pymongoarrow no longer works when pandas is not installed (#105)
* ARROW-126 pymongoarrow no longer works when pandas is not installed * cleanup * syntax
1 parent 39bf912 commit cf0cdf0

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

.github/workflows/test-python.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,15 @@ jobs:
7171
python -m pip install -U pip
7272
- name: Install pymongoarrow
7373
run: |
74-
# Install the library
75-
LIBBSON_INSTALL_DIR=$(pwd)/libbson python -m pip install -vvv -e ".[test]"
74+
# Install the library with no deps
75+
LIBBSON_INSTALL_DIR=$(pwd)/libbson python -m pip install -vv -e "."
76+
- name: Ensure imports with no test deps
77+
run: |
78+
python -c "from pymongoarrow.monkey import patch_all; patch_all()"
7679
- name: Run tests
7780
run: |
81+
# Install the test deps
82+
LIBBSON_INSTALL_DIR=$(pwd)/libbson python -m pip install -vv -e ".[test]"
7883
LD_LIBRARY_PATH=$(pwd)/libbson/lib python -m unittest discover test -v
7984
- name: Check the manifest
8085
run: |

bindings/python/pymongoarrow/api.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,19 @@
1717
import pymongo.errors
1818
from bson import encode
1919
from bson.raw_bson import RawBSONDocument
20-
from numpy import ndarray
21-
from pandas import DataFrame
2220
from pyarrow import Schema as ArrowSchema
2321
from pyarrow import Table
22+
23+
try:
24+
from numpy import ndarray
25+
except ImportError:
26+
ndarray = None
27+
28+
try:
29+
from pandas import DataFrame
30+
except ImportError:
31+
DataFrame = None
32+
2433
from pymongo.bulk import BulkWriteError
2534
from pymongo.common import MAX_WRITE_BATCH_SIZE
2635
from pymongoarrow.context import PyMongoArrowContext
@@ -295,7 +304,7 @@ def _tabular_generator(tabular):
295304
for i in tabular.to_batches():
296305
for row in i.to_pylist():
297306
yield row
298-
elif isinstance(tabular, DataFrame):
307+
elif DataFrame is not None and isinstance(tabular, DataFrame):
299308
for row in tabular.to_dict("records"):
300309
yield row
301310
elif isinstance(tabular, dict):
@@ -325,11 +334,12 @@ def write(collection, tabular):
325334
tab_size = len(tabular)
326335
if isinstance(tabular, Table):
327336
_validate_schema(tabular.schema.types)
328-
elif isinstance(tabular, DataFrame):
337+
elif DataFrame is not None and isinstance(tabular, DataFrame):
329338
_validate_schema(ArrowSchema.from_pandas(tabular).types)
330339
elif (
331340
isinstance(tabular, dict)
332341
and len(tabular.values()) >= 1
342+
and ndarray is not None
333343
and all([isinstance(i, ndarray) for i in tabular.values()])
334344
):
335345
_validate_schema([i.dtype for i in tabular.values()])

0 commit comments

Comments
 (0)