选择两个日期之间的 DataFrame 行 [英] Select DataFrame rows between two dates
问题描述
我正在从 csv 创建一个 DataFrame,如下所示:
I am creating a DataFrame from a csv as follows:
stock = pd.read_csv('data_in/' + filename + '.csv', skipinitialspace=True)
DataFrame 有一个日期列.有没有办法创建一个新的 DataFrame(或只是覆盖现有的),它只包含日期值落在指定日期范围内或两个指定日期值之间的行?
The DataFrame has a date column. Is there a way to create a new DataFrame (or just overwrite the existing one) which only contains rows with date values that fall within a specified date range or between two specified date values?
推荐答案
有两种可能的解决方案:
There are two possible solutions:
- 使用布尔掩码,然后使用
df.loc[mask]
- 将日期列设置为DatetimeIndex,然后使用
df[start_date : end_date]
使用布尔掩码:
确保 df['date']
是一个数据类型为 datetime64[ns]
的系列:
Ensure df['date']
is a Series with dtype datetime64[ns]
:
df['date'] = pd.to_datetime(df['date'])
制作一个布尔掩码.start_date
和 end_date
可以是 datetime.datetime
s,np.datetime64
s、pd.Timestamp
s,甚至日期时间字符串:
Make a boolean mask. start_date
and end_date
can be datetime.datetime
s,
np.datetime64
s, pd.Timestamp
s, or even datetime strings:
#greater than the start date and smaller than the end date
mask = (df['date'] > start_date) & (df['date'] <= end_date)
选择子DataFrame:
Select the sub-DataFrame:
df.loc[mask]
或重新分配给df
df = df.loc[mask]
<小时>
例如
import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.random((200,3)))
df['date'] = pd.date_range('2000-1-1', periods=200, freq='D')
mask = (df['date'] > '2000-6-1') & (df['date'] <= '2000-6-10')
print(df.loc[mask])
收益
0 1 2 date
153 0.208875 0.727656 0.037787 2000-06-02
154 0.750800 0.776498 0.237716 2000-06-03
155 0.812008 0.127338 0.397240 2000-06-04
156 0.639937 0.207359 0.533527 2000-06-05
157 0.416998 0.845658 0.872826 2000-06-06
158 0.440069 0.338690 0.847545 2000-06-07
159 0.202354 0.624833 0.740254 2000-06-08
160 0.465746 0.080888 0.155452 2000-06-09
161 0.858232 0.190321 0.432574 2000-06-10
<小时>
使用 DatetimeIndex:
如果您要按日期进行大量选择,则设置日期可能会更快date
列作为索引第一.然后您可以使用按日期选择行df.loc[start_date:end_date]
.
If you are going to do a lot of selections by date, it may be quicker to set the
date
column as the index first. Then you can select rows by date using
df.loc[start_date:end_date]
.
import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.random((200,3)))
df['date'] = pd.date_range('2000-1-1', periods=200, freq='D')
df = df.set_index(['date'])
print(df.loc['2000-6-1':'2000-6-10'])
收益
0 1 2
date
2000-06-01 0.040457 0.326594 0.492136 # <- includes start_date
2000-06-02 0.279323 0.877446 0.464523
2000-06-03 0.328068 0.837669 0.608559
2000-06-04 0.107959 0.678297 0.517435
2000-06-05 0.131555 0.418380 0.025725
2000-06-06 0.999961 0.619517 0.206108
2000-06-07 0.129270 0.024533 0.154769
2000-06-08 0.441010 0.741781 0.470402
2000-06-09 0.682101 0.375660 0.009916
2000-06-10 0.754488 0.352293 0.339337
虽然 Python 列表索引,例如seq[start:end]
包括 start
但不包括 end
,相比之下,Pandas df.loc[start_date : end_date]
如果它们在索引中,则在结果中包括两个 端点.但是,start_date
和 end_date
都不必在索引中.
While Python list indexing, e.g. seq[start:end]
includes start
but not end
, in contrast, Pandas df.loc[start_date : end_date]
includes both end-points in the result if they are in the index. Neither start_date
nor end_date
has to be in the index however.
还要注意 pd.read_csv
有一个 parse_dates
参数,您可以使用它来将 date
列解析为 datetime64
s.因此,如果您使用 parse_dates
,则不需要使用 df['date'] = pd.to_datetime(df['date'])
.
Also note that pd.read_csv
has a parse_dates
parameter which you could use to parse the date
column as datetime64
s. Thus, if you use parse_dates
, you would not need to use df['date'] = pd.to_datetime(df['date'])
.
这篇关于选择两个日期之间的 DataFrame 行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!