Python pandas timeline chart with space

I am trying to build a pandas DataFrame with TimeStamp indices that have time gaps in their metrics. Using pandas.plot () results in linear interpolation between the last TimeStamp of the previous segment and the first TimeStamp of the next. I do not want linear interpolation and I do not want the empty space between two date segments. Is there any way to do this?

Suppose a DataFrame with a TimeStamp index:

>>> import numpy as np
>>> import pandas as pd
>>> import matplotlib.pyplot as plt
>>> df = pd.DataFrame(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
>>> df = df.cumsum()

Now let's take two time fragments and draw it:

>>> df = pd.concat([df['Jan 2000':'Aug 2000'], df['Jan 2001':'Aug 2001']])
>>> df.plot()
>>> plt.show()

The resulting graph has an interpolation line connecting TimeStamps spanning the gap. I can’t understand how to upload photos to this machine, but these photos from the Google Group show my problem (interpolation .jpg, without interpolation. Jpg and without gaps.jpg). I can recreate the first as shown above. The second is achievable by replacing all the gap values ​​with NaN (see also this question ). How can I get the third version, where the time span is omitted?

+5
source share
2 answers

Try:

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

Skip the gap

You might want to customize tags and tags.

EDIT

This works for me using pandas 0.17.1 and numpy 1.10.4.

, , DatetimeIndex , . , str. x=df.index.astype(str) /numpy/ , :

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

, , .

+5

DateTimeIndex TimeStamp, 0.24.2 , DatetimeIndex .

df = pd.read_sql_query(sql, sql_engine)
df.set_index('date'), inplace=True)
df.index = df.index.map(str)
0

Source: https://habr.com/ru/post/1626531/


All Articles