Skip to content

Commit

Permalink
Partial LuxSeries support for lux-org#66
Browse files Browse the repository at this point in the history
Former-commit-id: a4d777d
  • Loading branch information
dorisjlee committed Aug 18, 2020
1 parent c3be6a9 commit a654357
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lux/core/frame.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pandas as pd
from lux.core.series import LuxSeries
from lux.vis.Clause import Clause
from lux.vis.Vis import Vis
from lux.vis.VisList import VisList
Expand Down Expand Up @@ -41,6 +42,18 @@ def __init__(self,*args, **kw):
self.cardinality = None
self.min_max = None
self.pre_aggregated = None

@property
def _constructor(self):
return LuxDataFrame

# @property
# def _constructor_sliced(self):
# def f(*args, **kwargs):
# # adapted from https://github.com/pandas-dev/pandas/issues/13208#issuecomment-326556232
# return LuxSeries(*args, **kwargs).__finalize__(self, method='inherit')
# return f

def maintain_metadata(self):
if (not hasattr(self,"_metadata_fresh") or not self._metadata_fresh ): # Check that metadata has not yet been computed
if (len(self)>0): #only compute metadata information if the dataframe is non-empty
Expand Down Expand Up @@ -558,6 +571,10 @@ def _repr_html_(self):
warnings.warn("\nLux can not operate on an empty dataframe.\nPlease check your input again.\n",stacklevel=2)
display(self.display_pandas())
return
if (len(self.columns)<=1):
warnings.warn("\nLux defaults to Pandas when there is only a single column.",stacklevel=2)
display(self.display_pandas())
return
self.maintain_metadata()

if (self._intent!=[] and not self._compiled):
Expand Down
19 changes: 19 additions & 0 deletions lux/core/series.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pandas as pd
class LuxSeries(pd.Series):
# _metadata = ['name','_intent','data_type_lookup','data_type',
# 'data_model_lookup','data_model','unique_values','cardinality',
# 'min_max','plot_config', '_current_vis','_widget', '_recommendation']
def __init__(self,*args, **kw):
super(LuxSeries, self).__init__(*args, **kw)
@property
def _constructor(self):
return LuxSeries

@property
def _constructor_expanddim(self):
from lux.core.frame import LuxDataFrame
# def f(*args, **kwargs):
# # adapted from https://github.com/pandas-dev/pandas/issues/13208#issuecomment-326556232
# return LuxDataFrame(*args, **kwargs).__finalize__(self, method='inherit')
# return f
return LuxDataFrame
14 changes: 14 additions & 0 deletions tests/test_pandas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from .context import lux
import pytest
import pandas as pd

# def test_df_to_series():
# # Ensure metadata is kept when going from df to series
# df = pd.read_csv("lux/data/car.csv")
# df._repr_html_() # compute metadata
# assert df.cardinality is not None
# series = df["Weight"]
# assert isinstance(series,lux.core.series.LuxSeries), "Derived series is type LuxSeries."
# assert df["Weight"]._metadata == ['name','_intent', 'data_type_lookup', 'data_type', 'data_model_lookup', 'data_model', 'unique_values', 'cardinality', 'min_max', 'plot_config', '_current_vis', '_widget', '_recommendation'], "Metadata is lost when going from Dataframe to Series."
# assert df.cardinality is not None, "Metadata is lost when going from Dataframe to Series."
# assert series.name == "Weight", "Pandas Series original `name` property not retained."

0 comments on commit a654357

Please sign in to comment.