Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: plotting with DatetimeIndex containing NaT #12405

Closed
jorisvandenbossche opened this issue Feb 21, 2016 · 0 comments · Fixed by #14540
Closed

BUG: plotting with DatetimeIndex containing NaT #12405

jorisvandenbossche opened this issue Feb 21, 2016 · 0 comments · Fixed by #14540
Labels
Bug Datetime Datetime data dtype Visualization plotting

Comments

@jorisvandenbossche
Copy link
Member

jorisvandenbossche commented Feb 21, 2016

xref #8914

In [1]: %matplotlib
Using matplotlib backend: Qt4Agg

In [2]: df = pd.DataFrame({'date': pd.date_range('2016-01-01', periods=5), 'vals
': range(5)})

In [3]: df.loc[2, 'date'] = np.nan

In [4]: s = df.set_index('date')['vals']

In [5]: s
Out[5]:
date
2016-01-01    0
2016-01-02    1
NaT           2
2016-01-04    3
2016-01-05    4
Name: vals, dtype: int64

In [6]: ax = s.plot()

In [11]: ax.get_lines()[0].get_data()
Out[11]:
(array([datetime.datetime(2016, 1, 1, 0, 0),
        datetime.datetime(2016, 1, 2, 0, 0),
        datetime.datetime(2016, 1, 4, 0, 0),
        datetime.datetime(2016, 1, 5, 0, 0), NaT], dtype=object),
 array([0, 1, 3, 4, 2], dtype=int64))

In [44]: ax.get_lines()[0].get_xydata()
Out[44]:
array([[  7.35964000e+05,   0.00000000e+00],
       [  7.35965000e+05,   1.00000000e+00],
       [  7.35967000e+05,   3.00000000e+00],
       [  7.35968000e+05,   4.00000000e+00],
       [  6.12411009e+05,   2.00000000e+00]])

So this gives you a plot with one of the values in the year 1677, September 22, so the minimum possible Timestamp.

This is of course not correct, but there are two things:

  • NaT gets converted to Timestamp.min
  • the order of the values is changed, as the NaT is put at the end

There is a related issue about plt.plot(pd.NaT) erroring (#9253), but in this case it is pandas code that gives wrong results, so we have more control over this. I find it also strange that this does not error as in the pd.NaT case, but converts the NaT to Timestamp.min (but didn't look into detail)

@jorisvandenbossche jorisvandenbossche added Bug Datetime Datetime data dtype Visualization plotting labels Feb 21, 2016
@jreback jreback added this to the Next Major Release milestone Feb 22, 2016
tsdlovell added a commit to tsdlovell/pandas that referenced this issue Oct 30, 2016
tsdlovell added a commit to tsdlovell/pandas that referenced this issue Oct 30, 2016
tsdlovell added a commit to tsdlovell/pandas that referenced this issue Oct 31, 2016
tsdlovell added a commit to tsdlovell/pandas that referenced this issue Oct 31, 2016
tsdlovell added a commit to tsdlovell/pandas that referenced this issue Jan 28, 2017
tsdlovell added a commit to tsdlovell/pandas that referenced this issue Jan 28, 2017
tsdlovell pushed a commit to tsdlovell/pandas that referenced this issue Apr 21, 2017
…._get_xticks

TST: add test for fix of pandas-dev#12405
DOC: update whatsnew/v0.20.0.txt
tsdlovell pushed a commit to tsdlovell/pandas that referenced this issue May 20, 2017
…._get_xticks

TST: add test for fix of pandas-dev#12405
DOC: update whatsnew/v0.20.0.txt
TomAugspurger pushed a commit that referenced this issue May 20, 2017
…ks (#14540)

TST: add test for fix of #12405
DOC: update whatsnew/v0.20.2.txt
pcluo pushed a commit to pcluo/pandas that referenced this issue May 22, 2017
…._get_xticks (pandas-dev#14540)

TST: add test for fix of pandas-dev#12405
DOC: update whatsnew/v0.20.2.txt
stangirala pushed a commit to stangirala/pandas that referenced this issue Jun 11, 2017
…._get_xticks (pandas-dev#14540)

TST: add test for fix of pandas-dev#12405
DOC: update whatsnew/v0.20.2.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype Visualization plotting
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants