Skip to content

Commit

Permalink
Improve handling of categorical dates
Browse files Browse the repository at this point in the history
  • Loading branch information
mwaskom committed Jan 5, 2021
1 parent 0dce5fa commit 10d007c
Showing 1 changed file with 3 additions and 10 deletions.
13 changes: 3 additions & 10 deletions seaborn/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,6 @@ def __init__(
# conversion because we want to respect the numeric ordering rule.
order = categorical_order(cat_data, order)

# XXX XXX TODO
# This is failing on dates because categorical_order calls
# Series.unique which returns a numpy array and numpy datetime objects
# convert to string differently from pandas ones. Perhaps the best way
# to deal with that is to augment categorical_order with special date
# handling?

# XXX Then convert data to strings. This is because in matplotlib,
# "categorical" data really mean "string" data. We want a more general
# approach for "fixed width" positioning, which will likely require
Expand All @@ -99,7 +92,7 @@ def __init__(
# XXX should we try to auto-format dates to a nice representation,
# and perhaps expose a formatter object to do it?
cat_data = cat_data.astype(str)
order = list(map(str, order))
order = pd.Index(order).astype(str)

# XXX here we are inserting the levels of the categorical variable
# into the var_levels object so that it can be used in iter_data.
Expand All @@ -126,15 +119,15 @@ def __init__(
# overriding a given order list, which isn't what the logic of
# categorical_order says should happen, but does because _attach doesn't
# get the order argument. This *should* resolve that problem.
if cat_data.cat.categories.to_list() != order:
if not cat_data.cat.categories.equals(order):
cat_data = cat_data.cat.set_categories(order, ordered=True)

self.plot_data[self.cat_axis] = cat_data

# XXX Need to decide if this is something that should hang around on the plotter
# adding it now so that function bodies can make use of possibly type-converted
# order list.
self.order = order
self.order = order.to_list()

@property
def cat_axis(self):
Expand Down

0 comments on commit 10d007c

Please sign in to comment.