簡體   English   中英

在 Pandas 中設置 TimeDeltaIndex 的 X 軸限制

[英]Setting X-axis limit on TimeDeltaIndex in Pandas

我有一個帶有 TimeDeltaIndex 的 Pandas 數據幀(邏輯上表示數據與經過時間的關系),並希望 plot 並通過將起點和終點設置為 Timedeltas 來設置特定的 x 軸限制。 但是我得到一個錯誤。 我知道我可以通過多種方式實現相同的目標,例如使用數字 X 值,或轉換為具有任意開始時間的時間戳。 但我很好奇為什么這種方法不起作用。 此外,在這個主題上,TimeDeltaIndex 是為此目的而設計的 - 代表數據與經過的時間嗎? 還是有更好(更靈活方便)的工具?

import pandas as pd
import numpy as np

data=np.random.random(10)
index=pd.timedelta_range(start="00:00:00", periods=10, freq='T')

df=pd.DataFrame(data, index)

start=pd.Timedelta(value=2, unit='T')
end=pd.Timedelta(value=7, unit='T')

df.plot(xlim=(start, end))

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

您不需要傳遞 Timedelta 值:

import pandas as pd
import numpy as np

data=np.random.random(10)
index=pd.timedelta_range(start="00:00:00", periods=10, freq='T')

df=pd.DataFrame(data, index)

start=0 # i.e the first index value
end=40 # i.e the 40th index value

df.plot(xlim=(start, end))

這對我有用!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM