Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从数据框中消除特定日期的最快方法_Python_Datetime_Pandas_Indexing_Data Science - Fatal编程技术网

Python 从数据框中消除特定日期的最快方法

Python 从数据框中消除特定日期的最快方法,python,datetime,pandas,indexing,data-science,Python,Datetime,Pandas,Indexing,Data Science,我正在处理一个大数据帧,我正在努力寻找一种有效的方法来消除特定的日期。请注意,我正试图消除特定日期的任何测量值 熊猫有一个很好的功能,你可以调用: df.ix['2016-04-22'] 从那天起把所有的行都拉出来。但如果我想删除“2016-04-22”中的所有行,该怎么办 我想要这样一个函数: df.ix[~'2016-04-22'] df[~((df['Timestamp'] < r+pd.Timedelta("1 day")) & (df['Timestamp'] &g

我正在处理一个大数据帧,我正在努力寻找一种有效的方法来消除特定的日期。请注意,我正试图消除特定日期的任何测量值

熊猫有一个很好的功能,你可以调用:

df.ix['2016-04-22'] 
从那天起把所有的行都拉出来。但如果我想删除“2016-04-22”中的所有行,该怎么办

我想要这样一个函数:

df.ix[~'2016-04-22']
df[~((df['Timestamp'] < r+pd.Timedelta("1 day")) & (df['Timestamp'] > r))]
(但这不起作用)

另外,如果我想删除日期列表,该怎么办

现在,我有以下解决方案:

import numpy as np
import pandas as pd
from numpy import random

###Create a sample data frame

dates = [pd.Timestamp('2016-04-25 06:48:33'), pd.Timestamp('2016-04-27 15:33:23'), pd.Timestamp('2016-04-23 11:23:41'), pd.Timestamp('2016-04-28    12:08:20'), pd.Timestamp('2016-04-21 15:03:49'), pd.Timestamp('2016-04-23 08:13:42'), pd.Timestamp('2016-04-27 21:18:22'), pd.Timestamp('2016-04-27 18:08:23'), pd.Timestamp('2016-04-27 20:48:22'), pd.Timestamp('2016-04-23 14:08:41'), pd.Timestamp('2016-04-27 02:53:26'), pd.Timestamp('2016-04-25 21:48:31'), pd.Timestamp('2016-04-22 12:13:47'), pd.Timestamp('2016-04-27 01:58:26'), pd.Timestamp('2016-04-24 11:48:37'), pd.Timestamp('2016-04-22 08:38:46'), pd.Timestamp('2016-04-26 13:58:28'), pd.Timestamp('2016-04-24 15:23:36'), pd.Timestamp('2016-04-22 07:53:46'), pd.Timestamp('2016-04-27 23:13:22')]

values = random.normal(20, 20, 20)

df = pd.DataFrame(index=dates, data=values, columns ['values']).sort_index()

### This is the list of dates I want to remove

removelist = ['2016-04-22', '2016-04-24']
这个for循环基本上获取我想要删除的日期的索引,然后从主数据帧的索引中删除它,然后从数据帧中积极地选择剩余的日期(即:好日期)

for r in removelist:
    elimlist = df.ix[r].index.tolist()
    ind = df.index.tolist()
    culind = [i for i in ind if i not in elimlist]
    df = df.ix[culind]
还有更好的吗

我还尝试按四舍五入的日期+1天编制索引,因此类似这样:

df.ix[~'2016-04-22']
df[~((df['Timestamp'] < r+pd.Timedelta("1 day")) & (df['Timestamp'] > r))]
df[~(df['Timestamp']r))]
但这会变得非常麻烦,(在一天结束时)当我需要消除n个特定日期时,我仍然会使用for循环


一定有更好的办法!对吗?也许吧

您可以使用列表创建布尔掩码

>>> df[[d.date() not in pd.to_datetime(removelist) for d in df.index]]
                        values
2016-04-21 15:03:49  28.059520
2016-04-23 08:13:42 -22.376577
2016-04-23 11:23:41  40.350252
2016-04-23 14:08:41  14.557856
2016-04-25 06:48:33  -0.271976
2016-04-25 21:48:31  20.156240
2016-04-26 13:58:28  -3.225795
2016-04-27 01:58:26  51.991293
2016-04-27 02:53:26  -0.867753
2016-04-27 15:33:23  31.585201
2016-04-27 18:08:23  11.639641
2016-04-27 20:48:22  42.968156
2016-04-27 21:18:22  27.335995
2016-04-27 23:13:22  13.120088
2016-04-28 12:08:20  53.730511

与@Alexander的想法相同,但使用和的属性:

时间:

%timeit df.loc[~np.in1d(df.index.date, pd.to_datetime(removelist).date), :]
1000 loops, best of 3: 1.42 ms per loop

%timeit df[[d.date() not in pd.to_datetime(removelist) for d in df.index]]
100 loops, best of 3: 3.25 ms per loop

伟大的选择!谢谢亚历山大!令人惊叹的!很好用!非常感谢您的回复!