Python pandas plot time-series with gap

前端 未结 2 2018
春和景丽
春和景丽 2021-01-02 20:58

I am trying to plot a pandas DataFrame with TimeStamp indizes that has a time gap in its indizes. Using pandas.plot() results in linear interpolation between the last TimeSt

相关标签:
2条回答
  • 2021-01-02 21:35

    Try:

    df.plot(x=df.index.astype(str))
    

    You may want to customize ticks and tick labels.

    EDIT

    That works for me using pandas 0.17.1 and numpy 1.10.4.

    All you really need is a way to convert the DatetimeIndex to another type which is not datetime-like. In order to get meaningful labels I chose str. If x=df.index.astype(str) does not work with your combination of pandas/numpy/whatever you can try other options:

    df.index.to_series().dt.strftime('%Y-%m-%d')
    df.index.to_series().apply(lambda x: x.strftime('%Y-%m-%d'))
    ...
    

    I realized that resetting the index is not necessary so I removed that part.

    0 讨论(0)
  • 2021-01-02 21:37

    In my case I had DateTimeIndex objects instead of TimeStamp, but the following works for me in pandas 0.24.2 to eliminate the time series gaps after converting the DatetimeIndex objects to string.

    df = pd.read_sql_query(sql, sql_engine)
    df.set_index('date'), inplace=True)
    df.index = df.index.map(str)
    
    0 讨论(0)
提交回复
热议问题