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

refactor: Remove direct pandas import #6476

Draft
wants to merge 9 commits into
base: main
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
4 changes: 1 addition & 3 deletions holoviews/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from datetime import date, datetime

import pandas as pd

from .boundingregion import *
from .data import *
from .dimension import *
Expand Down Expand Up @@ -31,7 +29,7 @@
Dimension.type_formatters[np.datetime64] = '%Y-%m-%d %H:%M:%S'
Dimension.type_formatters[datetime] = '%Y-%m-%d %H:%M:%S'
Dimension.type_formatters[date] = '%Y-%m-%d'
Dimension.type_formatters[pd.Timestamp] = "%Y-%m-%d %H:%M:%S"
# Dimension.type_formatters['pandas.Timestamp'] = "%Y-%m-%d %H:%M:%S" # TODO: Implement this


def public(obj):
Expand Down
6 changes: 4 additions & 2 deletions holoviews/core/data/cudf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
from itertools import product

import numpy as np
import pandas as pd
from pandas.api.types import is_numeric_dtype

from .. import util
from ..dimension import dimension_name
Expand Down Expand Up @@ -49,6 +47,7 @@ def applies(cls, obj):
@classmethod
def init(cls, eltype, data, kdims, vdims):
import cudf
import pandas as pd

element_params = eltype.param.objects()
kdim_param = element_params['kdims']
Expand Down Expand Up @@ -276,6 +275,9 @@ def add_dimension(cls, dataset, dimension, dim_pos, values, vdim):

@classmethod
def aggregate(cls, dataset, dimensions, function, **kwargs):
import pandas as pd
from pandas.api.types import is_numeric_dtype

data = dataset.data
cols = [d.name for d in dataset.kdims if d in dimensions]
vdims = dataset.dimensions('value', label='name')
Expand Down
2 changes: 1 addition & 1 deletion holoviews/core/data/dask.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import sys

import numpy as np
import pandas as pd

from .. import util
from ..dimension import Dimension
Expand Down Expand Up @@ -252,6 +251,7 @@ def aggregate(cls, dataset, dimensions, function, **kwargs):
agg = getattr(reindexed, inbuilts[function.__name__])()
else:
raise NotImplementedError
import pandas as pd
df = pd.DataFrame(agg.compute()).T

dropped = []
Expand Down
2 changes: 1 addition & 1 deletion holoviews/core/data/ibis.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from .interface import DataError, Interface
from .util import cached

IBIS_VERSION = util._no_import_version("ibis-framework")
IBIS_VERSION = util.versions._no_import_version("ibis-framework")
IBIS_GE_4_0_0 = IBIS_VERSION >= (4, 0, 0)
IBIS_GE_5_0_0 = IBIS_VERSION >= (5, 0, 0)
IBIS_GE_9_5_0 = IBIS_VERSION >= (9, 5, 0)
Expand Down
20 changes: 17 additions & 3 deletions holoviews/core/data/pandas.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys

import numpy as np
import pandas as pd
from pandas.api.types import is_numeric_dtype

from .. import util
from ..dimension import Dimension, dimension_name
Expand All @@ -23,13 +23,26 @@ class PandasAPI:
...
"""

pd = None # Set when PandasInterface.applies is successful
Copy link
Member Author

Choose a reason for hiding this comment

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

Does not seem to work with test-gpu 🤷‍♂️


class PandasInterface(Interface, PandasAPI):

types = (pd.DataFrame,)
types = ()

datatype = 'dataframe'

@classmethod
def loaded(cls):
return 'pandas' in sys.modules

@classmethod
def applies(cls, obj):
if not cls.loaded():
return False
global pd # noqa: PLW0603
import pandas as pd
return isinstance(obj, (pd.DataFrame, pd.Series))

@classmethod
def dimension_type(cls, dataset, dim):
return cls.dtype(dataset, dim).type
Expand Down Expand Up @@ -289,6 +302,7 @@ def aggregate(cls, dataset, dimensions, function, **kwargs):
c for c in reindexed.columns if c not in cols
]
else:
from pandas.api.types import is_numeric_dtype
numeric_cols = [
c for c, d in zip(reindexed.columns, reindexed.dtypes)
if is_numeric_dtype(d) and c not in cols
Expand Down
4 changes: 3 additions & 1 deletion holoviews/core/data/spatialpandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from collections import defaultdict

import numpy as np
import pandas as pd

from ..dimension import dimension_name
from ..util import isscalar, unique_array, unique_iterator
Expand Down Expand Up @@ -98,6 +97,8 @@ def init(cls, eltype, data, kdims, vdims):
elif 'geometry' not in data:
cls.geo_column(data)

import pandas as pd

index_names = data.index.names if isinstance(data, pd.DataFrame) else [data.index.name]
if index_names == [None]:
index_names = ['index']
Expand Down Expand Up @@ -865,6 +866,7 @@ def from_multi(eltype, data, kdims, vdims):
Returns:
A GeoDataFrame containing in the list based format.
"""
import pandas as pd
from spatialpandas import GeoDataFrame

xname, yname = (kd.name for kd in kdims[:2])
Expand Down
3 changes: 2 additions & 1 deletion holoviews/core/data/xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import types

import numpy as np
import pandas as pd

from .. import util
from ..dimension import Dimension, asdim, dimension_name
Expand Down Expand Up @@ -654,6 +653,8 @@ def dframe(cls, dataset, dimensions):

@classmethod
def sample(cls, dataset, samples=None):
import pandas as pd

if samples is None:
samples = []
names = [kd.name for kd in dataset.kdims]
Expand Down
3 changes: 2 additions & 1 deletion holoviews/core/element.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from itertools import groupby

import numpy as np
import pandas as pd
import param

from .dimension import Dimensioned, ViewableElement, asdim
Expand Down Expand Up @@ -210,6 +209,8 @@ def dframe(self, dimensions=None, multi_index=False):
Returns:
DataFrame of columns corresponding to each dimension
"""
import pandas as pd

if dimensions is None:
dimensions = [d.name for d in self.dimensions()]
else:
Expand Down
3 changes: 2 additions & 1 deletion holoviews/core/ndmapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from operator import itemgetter

import numpy as np
import pandas as pd
import param

from . import util
Expand Down Expand Up @@ -891,6 +890,8 @@ def dframe(self, dimensions=None, multi_index=False):
Returns:
DataFrame of columns corresponding to each dimension
"""
import pandas as pd

if dimensions is None:
outer_dimensions = self.kdims
inner_dimensions = None
Expand Down
Loading
Loading