question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Some categorical plots fail when y is datetime

See original GitHub issue

Categorical plots are not interchangeable when the dependent variable is np.datetime. Apart from stripplot and swarmplot they all may fail. Consider the following code:

import seaborn as sns
import numpy as np
dates = np.arange('2020-03', '2020-04', dtype='datetime64[D]')
cats = np.repeat("cat", len(dates))
for func in [sns.stripplot, sns.swarmplot, sns.boxplot, sns.boxenplot, sns.violinplot, sns.barplot]:
    try:
         func(cats, dates)
    except Exception as ex:
         print(str(func), type(ex), ex)

The above code results with the following lines printed:

<function boxplot at 0x7f520e7af598> <class 'numpy.core._exceptions.UFuncTypeError'> ufunc 'add' cannot use operands with types dtype('<M8[ns]') and dtype('<M8[ns]')
<function boxenplot at 0x7f520e7af730> <class 'numpy.core._exceptions.UFuncTypeError'> ufunc 'multiply' cannot use operands with types dtype('<M8[ns]') and dtype('float64')
<function violinplot at 0x7f520e7af620> <class 'TypeError'> invalid type promotion
<function barplot at 0x7f520e7af8c8> <class 'numpy.core._exceptions.UFuncTypeError'> ufunc 'add' cannot use operands with types dtype('<M8[ns]') and dtype('<M8[ns]')

The problem is that datetime variable is not identified as categorical and also not identified as non-numeric and therefore specific plotters attempt to perform operations that expect numeric input. Categorical plots docstrings currently don’t mention datetime y (only datetime x).

Probably the easiest fix to maintain consistency between all categorical plots would be changing the following method to identify dates as non-numeric (perhaps using pandas.api.types.is_numeric_dtype). https://github.com/mwaskom/seaborn/blob/562fd90216d5529ab8f26b388168e6ef260d7e6c/seaborn/categorical.py#L330-L335 However this will also disable stripplot and swarmplot with dates.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
isomorphemecommented, Nov 13, 2020

I think I’m having a similar issue: I have a dataframe with a country categorical column, and a created_at datetime column. Using catplot(data, kind='swarm', x='country', y='created_at') works fine, but when I use kind='violin', Seaborn complains with the aforementioned TypeError: Neither the xnory variable appears to be numeric. I would expect otherwise, because I can still e.g. plot a distribution for all dates using distplot, and a violin plot does essentially that, just per category.

1reaction
MaozGelbartcommented, May 28, 2020

I’ll close this as the reason for failures is now clear for the user. Will open a separate issue with an example regarding qualitative colormap when hue is datetime. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Plotting datetime against categorical data (Y-axis)
In the plot, replace y with y_cat , and matplotlib throws an error: import pandas as pd import numpy as np import calendar,...
Read more >
Unable to plot image_url with datetime and categorical axis
Any idea how to make it render the image for datetime / categorical axis? Thanks! ... w and h arguments are in data...
Read more >
Why do I get this error while plotting this? - MATLAB Answers
The problem is that you are plotting year first, and the year you plot is pure numeric, 1998+something. Once you have plotted numeric...
Read more >
Visualizing categorical data — seaborn 0.12.1 documentation
Comparing distributions​​ As the size of the dataset grows, categorical scatter plots become limited in the information they can provide about the distribution ......
Read more >
Three common seaborn difficulties | by Michael Waskom
Notably, it will become possible to maintain the original scale of numeric (or datetime) data on the “categorical” axis. But you'll have to ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found