【发布时间】:2012-12-20 06:39:04
【问题描述】:
尝试在 pandas 中重新索引数据框时,我遇到了一个非常奇怪的行为。我的 Pandas 版本是 0.10.0,我使用 Python 2.7。 基本上,当我加载数据框时:
eurusd = pd.DataFrame.load('EUR_USD_30Min.df').drop_duplicates().dropna()
eurusd
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 119710 entries, 2003-02-02 17:30:00 to 2012-12-28 17:00:00
Data columns:
open 119710 non-null values
high 119710 non-null values
low 119710 non-null values
close 119710 non-null values
dtypes: float64(4)
然后我尝试在更大的日期范围内重新索引:
newindex = pd.DateRange(datetime.datetime(2002,1,1), datetime.datetime(2012,12,31), offset=pd.datetools.Minute(30))
newindex
<class 'pandas.tseries.index.DatetimeIndex'>
[2002-01-01 00:00:00, ..., 2012-12-31 00:00:00]
Length: 192817, Freq: 30T, Timezone: None
尝试重新索引数据框时出现奇怪的行为。如果我重新索引数据集的较大部分,我会收到此错误:
eurusd[29558:29560].reindex(index=newindex)
Exception: Reindexing only valid with uniquely valued Index objects
但是,如果我对上面的两个数据子集执行相同的操作,我不会收到错误:
这是第一个子集,没有问题,
eurusd[29558:29559].reindex(index=newindex)
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 192817 entries, 2002-01-01 00:00:00 to 2012-12-31 00:00:00
Freq: 30T
Data columns:
open 1 non-null values
high 1 non-null values
low 1 non-null values
close 1 non-null values
dtypes: float64(4)
这是第二个子集,仍然没有问题,
eurusd[29559:29560].reindex(index=newindex)
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 192817 entries, 2002-01-01 00:00:00 to 2012-12-31 00:00:00
Freq: 30T
Data columns:
open 1 non-null values
high 1 non-null values
low 1 non-null values
close 1 non-null values
dtypes: float64(4)
我真的为此发疯,无法理解其中的原因。看起来数据框从重复项和重复索引中“干净”....如果您愿意,我可以为数据框提供泡菜文件。
【问题讨论】:
-
eurusd.index.is_unique的输出是什么? (也许当时钟改变时,时间会重复,除非您将其包含在日期时间时区信息中) -
好吧,好问题!我试过了,它确实给了我 False ......但这如何解释各种数据集中的不同行为?
-
非常奇怪:如果我输入:eurusd[35700:35800].index.is_unique 它给了我 True 但如果我运行 eurusd[35700:].index.is_unique 我得到 False...跨度>
-
eurusd[35700:]中有一个重复的索引,一个想法是把它拆分更多找到它? :S -
eurusd[35700:35701].index.is_unique 给了我 True eurusd[35701:35800].index.is_unique 也给了我 True....帮助...