Skip to content

Commit

Permalink
Remove a pd
Browse files Browse the repository at this point in the history
  • Loading branch information
hoxbro committed Dec 27, 2022
1 parent 121a45d commit 3fb9eba
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 46 deletions.
45 changes: 20 additions & 25 deletions holoviews/core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -1150,22 +1150,18 @@ def unique_array(arr):
"""
if not len(arr):
return np.asarray(arr)
elif pd:
if isinstance(arr, np.ndarray) and arr.dtype.kind not in 'MO':
# Avoid expensive unpacking if not potentially datetime
return pd.unique(arr)

values = []
for v in arr:
if (isinstance(v, datetime_types) and
not isinstance(v, cftime_types)):
v = pd.Timestamp(v).to_datetime64()
values.append(v)
return pd.unique(values)
else:
arr = np.asarray(arr)
_, uniq_inds = np.unique(arr, return_index=True)
return arr[np.sort(uniq_inds)]

if isinstance(arr, np.ndarray) and arr.dtype.kind not in 'MO':
# Avoid expensive unpacking if not potentially datetime
return pd.unique(arr)

values = []
for v in arr:
if (isinstance(v, datetime_types) and
not isinstance(v, cftime_types)):
v = pd.Timestamp(v).to_datetime64()
values.append(v)
return pd.unique(values)


def match_spec(element, specification):
Expand Down Expand Up @@ -2095,15 +2091,14 @@ def dt_to_int(value, time_unit='us'):
"""
Converts a datetime type to an integer with the supplied time unit.
"""
if pd:
if isinstance(value, pd.Period):
value = value.to_timestamp()
if isinstance(value, pd.Timestamp):
try:
value = value.to_datetime64()
except Exception:
value = np.datetime64(value.to_pydatetime())
elif isinstance(value, cftime_types):
if isinstance(value, pd.Period):
value = value.to_timestamp()
if isinstance(value, pd.Timestamp):
try:
value = value.to_datetime64()
except Exception:
value = np.datetime64(value.to_pydatetime())
if isinstance(value, cftime_types):
return cftime_to_timestamp(value, time_unit)

# date class is a parent for datetime class
Expand Down
6 changes: 3 additions & 3 deletions holoviews/element/comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"""
from functools import partial
import numpy as np
import pandas as pd
from unittest.util import safe_repr
from unittest import TestCase
from numpy.testing import assert_array_equal, assert_array_almost_equal
Expand All @@ -28,7 +29,7 @@
HoloMap, Dimensioned, Layout, NdLayout, NdOverlay,
GridSpace, DynamicMap, GridMatrix, OrderedDict)
from ..core.options import Options, Cycle
from ..core.util import (pd, cast_array_to_int64, datetime_types, dt_to_int,
from ..core.util import (cast_array_to_int64, datetime_types, dt_to_int,
is_float)


Expand Down Expand Up @@ -115,8 +116,7 @@ def register(cls):
cls.equality_type_funcs[np.ma.masked_array] = cls.compare_arrays

# Pandas dataframe comparison
if pd:
cls.equality_type_funcs[pd.DataFrame] = cls.compare_dataframe
cls.equality_type_funcs[pd.DataFrame] = cls.compare_dataframe

# Dimension objects
cls.equality_type_funcs[Dimension] = cls.compare_dimensions
Expand Down
17 changes: 5 additions & 12 deletions holoviews/element/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,9 @@ def _aggregate_dataset(self, obj):
reindexed = concat_data.reindex([xdim, ydim], vdims)
if not reindexed:
agg = reindexed
elif pd:
df = PandasInterface.as_dframe(reindexed)
df = df.groupby([xdim, ydim], sort=False).first().reset_index()
agg = reindexed.clone(df)
else:
agg = reindexed.aggregate([xdim, ydim], reduce_fn)
df = PandasInterface.as_dframe(reindexed)
df = df.groupby([xdim, ydim], sort=False).first().reset_index()
agg = reindexed.clone(df)

# Convert data to a gridded dataset
for vdim in vdims:
Expand Down Expand Up @@ -225,12 +222,8 @@ def _process(self, obj, key=None):
raise ValueError("Must have at two dimensions to aggregate over"
"and one value dimension to aggregate on.")

if pd:
obj = Dataset(obj, datatype=['dataframe'])
return self._aggregate_dataset_pandas(obj)
else:
obj = Dataset(obj, datatype=['dictionary'])
return self._aggregate_dataset(obj)
obj = Dataset(obj, datatype=['dataframe'])
return self._aggregate_dataset_pandas(obj)


def circular_layout(nodes):
Expand Down
9 changes: 3 additions & 6 deletions holoviews/operation/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

from ..core import (Operation, NdOverlay, Overlay, GridMatrix,
HoloMap, Dataset, Element, Collator, Dimension)
from ..core.data import ArrayInterface, DictInterface, default_datatype
from ..core.data import ArrayInterface, DictInterface, PandasInterface, default_datatype
from ..core.data.util import dask_array_module
from ..core.util import (
group_sanitizer, label_sanitizer, pd, datetime_types, isfinite,
group_sanitizer, label_sanitizer, datetime_types, isfinite,
dt_to_int, isdatetime, is_dask_array, is_cupy_array, is_ibis_expr
)
from ..element.chart import Histogram, Scatter
Expand All @@ -24,10 +24,7 @@
from ..element.util import categorical_aggregate2d # noqa (API import)
from ..streams import RangeXY

column_interfaces = [ArrayInterface, DictInterface]
if pd:
from ..core.data import PandasInterface
column_interfaces.append(PandasInterface)
column_interfaces = [ArrayInterface, DictInterface, PandasInterface]


def identity(x,k): return x
Expand Down

0 comments on commit 3fb9eba

Please sign in to comment.