简体   繁体   English

Python Pandas datetime64 [ns]比较

[英]Python Pandas datetime64[ns] comparision

I'm trying to use indexing to select rows in my dataframe after the date 2011-01-01. 我正在尝试使用索引在2011年1月1日之后选择数据框中的行。 I used following line of code to return only part of dataframe that is after 2011-01-01 我使用以下代码行仅返回2011年1月1日之后的数据框的一部分

  df = df[df.Date > np.datetime64('2011-01-01 00:00:00')]

I don't get an error. 我没有错。 However, I only see dates that are of 2016 year nothing on 2011. When I manually open file I can see that there are plenty of entries starting with 2011 year. 但是,我只看到2016年的日期不算2011年的日期。当我手动打开文件时,可以看到从2011年开始有很多条目。

What do I do wrong here? 我在这里做什么错? Any ideas? 有任何想法吗?

Thanks! 谢谢!

Here is a sniped of data: enter image description here 这是一小段数据: 在此处输入图片描述

After importing your data, it looks like all the values of the Date column are still there, even after filtering. 导入数据后,即使过滤后,“ Date列的所有值仍看起来仍然存在。 It's just that your data is too large to be displayed fully on your console (take a look at pandas settings ). 只是您的数据太大而无法在控制台上完全显示(请查看pandas设置 )。 Therefore some of it is being (visually) truncated to fit the page. 因此,其中的一些内容(在视觉上)被截断以适合页面。

The trick to use convert the Date column to a pandas datetime object and handle the filtering from there: 使用技巧将Date列转换为pandas datetime对象并从那里处理过滤:

import pandas as pd

df = pd.read_csv('Crimes_-_2001_to_present.csv', header = 0)
df.Date = pd.to_datetime(df.Date)

filterer = df.Date > pd.to_datetime('2011-01-01 00:00:00')
df = df[filterer]

Now when you look at the 200th row in the Date column, you should get something: 现在,当您查看“ Date列中的第200行时,您应该得到一些信息:

df['Date'].iloc[200]
#Timestamp('2011-05-31 19:30:00')

At row 2000 in column Date : Date列2000中:

df['Date'].iloc[2000]
#Timestamp('2013-09-19 20:45:00')

In essence, everything is there. 本质上,一切都在那里。 Your console is perhaps too small to fit it all. 您的控制台可能太小,无法全部容纳。

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM