[英]Using 'datetime64[ns]' format for extraction from pandas dataframe
I have a dataframe which has elements as: 我有一个数据框,其元素如下:
df1[1:4]
Sims
2014-01-02 [51, 53, 51, 3...
2014-01-03 [56, 48, 64, ...
2014-01-04 [57, 45, 47, ...
The sims are list of 500 elements each. Sims是每个包含500个元素的列表。
I have another dataframe as: 我有另一个数据框为:
df2[1:4]
Date Month Day HE Year
DateTime
2012-01-01 02:00:00 2012-01-01 1.0 1.0 2.0 2012.0
2012-01-01 03:00:00 2012-01-01 1.0 1.0 3.0 2012.0
2012-01-01 04:00:00 2012-01-01 1.0 1.0 4.0 2012.0
I am trying the following in various configurations: 我正在各种配置中尝试以下操作:
df1[df2['Date']]
But it errors out complaining about time format difference between df1
index and df2['Date']
. 但是它错误地抱怨了
df1
索引和df2['Date']
之间的时间格式差异。 However, both have same time format as shown below. 但是,两者具有相同的时间格式,如下所示。
df1.index[1:4]
DatetimeIndex(['2014-01-02', '2014-01-03', '2014-01-04'], dtype='datetime64[ns]', freq=None)
df2['Date'][1:4].values
array(['2012-01-01T00:00:00.000000000', '2012-01-01T00:00:00.000000000',
'2012-01-01T00:00:00.000000000'], dtype='datetime64[ns]')
How do I make the following work: 我如何进行以下工作:
df1[df2['Date']]
Edit: Error message: 编辑:错误消息:
KeyError: "['2012-01-01T00:00:00.000000000' '2012-01-01T00:00:00.000000000'\n '2012-01-01T00:00:00.000000000' ..., '2016-12-31T00:00:00.000000000'\n '2016-12-31T00:00:00.000000000' '2016-12-31T00:00:00.000000000'] not in index"
df1[df2['Date']]
-type indexing tends to error in my experience if you are trying to index on rows instead of columns. 如果您尝试对行而不是列进行索引,则
df1[df2['Date']]
-类型索引在我的经验中倾向于出错。 The problem is presumably that you let pandas
guess over which axis you whish to slice, and this doesn't always pan out as desired. 问题可能出在让
pandas
猜测要切哪条轴上,而这并不总是能按需要平移。
You could try using a more explicit indexing method such as df1.loc[df2['Date'], :]
or df1.xs(df2['Date'], 0)
. 您可以尝试使用更明确的索引方法,例如
df1.loc[df2['Date'], :]
df1.xs(df2['Date'], 0)
df1.loc[df2['Date'], :]
或df1.xs(df2['Date'], 0)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.