Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import xarray as xr
if isinstance(data, xr.DataArray):
data = data.to_dataset(name=data.name or 'value')
if is_tabular(data):
if self.use_index and any(c for c in self.hover_cols if
c in self.indexes and
c not in data.columns):
data = data.reset_index()
# calculate any derived time
dimensions = []
for dimension in [x, y, self.by, self.hover_cols]:
if dimension is not None:
dimensions.extend(dimension if isinstance(dimension, list) else [dimension])
not_found = [dim for dim in dimensions if dim not in self.variables]
_, data = process_derived_datetime_pandas(data, not_found, self.indexes)
return data, x, y, z
x, y = indexes
elif kind in ('bar', 'barh'):
x, by = indexes
# Rename non-string columns
renamed = {c: str(c) for c in data.columns if not isinstance(c, basestring)}
if renamed:
self.data = self.data.rename(columns=renamed)
self.variables = indexes + list(self.data.columns)
# Reset groupby dimensions
groupby_index = [g for g in groupby if g in indexes]
if groupby_index:
self.data = self.data.reset_index(groupby_index)
not_found = [g for g in groupby if g not in list(self.data.columns)+indexes]
not_found, self.data = process_derived_datetime_pandas(self.data, not_found, indexes)
if groupby and not_found:
raise ValueError('The supplied groupby dimension(s) %s '
'could not be found, expected one or '
'more of: %s' % (not_found, list(self.data.columns)))
# Set data-level options
self.x = x
self.y = y
self.kind = kind or 'line'
self.datatype = datatype
self.gridded = gridded
self.gridded_data = gridded_data
self.use_dask = use_dask
self.indexes = indexes
if isinstance(by, (np.ndarray, pd.Series)):
self.data = self.data.assign(_by=by)
self.by = ['_by']
data = data.sort_values(x)
# set index to column if needed in hover_cols
if self.use_index and any(c for c in self.hover_cols if
c in self.indexes and
c not in data.columns):
data = data.reset_index()
# calculate any derived time
dimensions = []
for col in [x, y, self.by, self.hover_cols]:
if col is not None:
dimensions.extend(col if isinstance(col, list) else [col])
not_found = [dim for dim in dimensions if dim not in self.variables]
_, data = process_derived_datetime_pandas(data, not_found, self.indexes)
return data, x, y