简体   繁体   English

使用'datetime64 [ns]'格式从熊猫数据框中提取

[英]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.

相关问题 从 pandas dataframe datetime64[ns] 索引中删除时间 - Remove time from pandas dataframe datetime64[ns] index 如何将包含数据和datetime64 [ns]的列表与带有datetime64 [ns]索引的熊猫数据框合并 - How to merge a list containing data and datetime64[ns] with a pandas dataframe with datetime64[ns] index Pandas DataFrame-使用OLS /线性回归时,“无法从[datetime64 [ns]]到[float64]分配日期时间” - Pandas DataFrame - 'cannot astype a datetimelike from [datetime64[ns]] to [float64]' when using ols/linear regression 以datetime64 [ns]作为微秒精度的pandas dataFrame - pandas dataFrame with datetime64[ns] as Index with microsecond precision Pandas 数据帧类型 datetime64[ns] 在 Hive/Athena 中不起作用 - Pandas dataframe type datetime64[ns] is not working in Hive/Athena 在 pandas dataframe 中聚合 datetime64[ns] 和浮点列 - Aggregating datetime64[ns] and float columns in a pandas dataframe 如何将Pandas Datatime列从Pandas datetime64 [ns]转换为Pyodbc SQL_Timestamp - How to convert Pandas DataFrame column from Pandas datetime64[ns] to Pyodbc SQL_Timestamp 如何使用 datetime64[ns] 和 pandas 计算天数? - How to count days using datetime64[ns] with pandas? 从 Pandas 中的 datetime64[ns] 中提取年份但不是 int 类型 - Extract year from datetime64[ns] in Pandas but not int type 从datetime64 [ns]到对象的大熊猫(python) - pandas from datetime64[ns] to object (python)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM