Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OPT: make pandas (and may be scipy, ... anything else?) dependency optional #1282

Draft
wants to merge 3 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion requirements-min.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@
h5py==2.9 # support for setting attrs to lists of utf-8 added in 2.9
hdmf==2.1.0,<3
numpy==1.16
pandas==0.23
python-dateutil==2.7
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
h5py==2.10.0
hdmf==2.1.0
numpy==1.18.5
pandas==0.25.3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pandas is still a requirement though, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that having pandas as a requirement for install should not be an issue. If we "just" want to disable certain functionality to speed-up import, then an alternative way would be to use environment variables such that, e.g., if PYNWB_NOPANDAS is set then you would not import Pandas, i.e., the user would have to explicitly set the variable before importing pynwb. Having a special "pynwb-lite" may be the "cleaner" variant but I'm afraid that this will ultimately add a lot of work and I don't think we have sufficient resources right now to support such an effort.

python-dateutil==2.8.1
7 changes: 5 additions & 2 deletions src/pynwb/core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from h5py import RegionReference
import numpy as np
import pandas as pd

from hdmf import Container, Data, DataRegion, get_region_slicer
from hdmf.container import AbstractContainer, MultiContainerInterface as hdmf_MultiContainerInterface
Expand Down Expand Up @@ -222,12 +221,14 @@ def to_dataframe(self):
'''Produce a pandas DataFrame containing this table's data.
'''

import pandas as pd
data = {colname: self[colname] for ii, colname in enumerate(self.columns)}
return pd.DataFrame(data)

@classmethod
@docval(
{'name': 'df', 'type': pd.DataFrame, 'doc': 'input data'},
# TODO: "real" pd.DataFrame?
{'name': 'df', 'type': "pd.DataFrame", 'doc': 'input data'},
{'name': 'name', 'type': str, 'doc': 'the name of this container', 'default': None},
{
'name': 'extra_ok',
Expand All @@ -241,6 +242,8 @@ def from_dataframe(cls, **kwargs):
should match the columns defined on the NWBTable subclass.
'''

import pandas as pd

df, name, extra_ok = getargs('df', 'name', 'extra_ok', kwargs)

cls_cols = list([col['name'] for col in getattr(cls, '__columns__')])
Expand Down
11 changes: 8 additions & 3 deletions src/pynwb/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import copy as _copy

import numpy as np
import pandas as pd

from hdmf.utils import docval, getargs, call_docval_func, get_docval

Expand Down Expand Up @@ -669,7 +668,8 @@ def add_stimulus_template(self, timeseries):
self._add_stimulus_template_internal(timeseries)
self._update_sweep_table(timeseries)

@docval({'name': 'data', 'type': (np.ndarray, list, tuple, pd.DataFrame, DynamicTable, NWBContainer, ScratchData),
# TODO: bring it pd.DataFrame somehow without causing import of pandas, e.g. if not already loaded?
@docval({'name': 'data', 'type': (np.ndarray, list, tuple, DynamicTable, NWBContainer, ScratchData),
'help': 'the data to add to the scratch space'},
{'name': 'name', 'type': str,
'help': 'the name of the data. Only used when passing in numpy.ndarray, list, or tuple',
Expand All @@ -683,7 +683,12 @@ def add_stimulus_template(self, timeseries):
def add_scratch(self, **kwargs):
'''Add data to the scratch space'''
data, name, notes = getargs('data', 'name', 'notes', kwargs)
if isinstance(data, (np.ndarray, pd.DataFrame, list, tuple)):
data_types = [np.ndarray, list, tuple]
if 'pandas' in sys.modules:
# delayed import and cannot pass DataFrame if pandas is not already loaded
import pandas as pd
data_types += [ pd.DataFrame ]
if isinstance(data, data_types):
if name is None:
raise ValueError('please provide a name for scratch data')
if isinstance(data, pd.DataFrame):
Expand Down