我有一个如下所示的数据框:
Date and Time Close dif
2015/01/01 17:00:00.211 2030.25 0.3
2015/01/01 17:00:02.456 2030.75 0.595137615
2015/01/01 23:55:01.491 2037.25 2.432613592
2015/01/02 00:02:01.955 2036.75 -0.4
2015/01/02 00:04:04.887 2036.5 -0.391144414
2015/01/02 15:14:56.207 2021.5 -4.732676608
2015/01/02 15:14:59.020 2021.5 -4.731171953
2015/01/02 15:30:00.020 2022 -4.228169436
2015/01/02 16:13:18.948 2021.25 -4.96153033
2015/01/02 16:15:00.000 2021 -5.210187988
2015/01/04 17:00:00.105 2020.5 0
2015/01/04 17:00:01.077 2021 0.423093923
如何修改索引,使当前日期从前一天的17:00:00开始,到15:15:00结束。 (可以消除15:15:00到17:00:00之间的数据)。
新数据框如下所示:
Date and Time Close dif
2015/01/02 17:00:00.211 2030.25 0.3
2015/01/02 17:00:02.456 2030.75 0.595137615
2015/01/02 23:55:01.491 2037.25 2.432613592
2015/01/02 00:02:01.955 2036.75 -0.4
2015/01/02 00:04:04.887 2036.5 -0.391144414
2015/01/02 15:14:56.207 2021.5 -4.732676608
2015/01/02 15:14:59.020 2021.5 -4.731171953
2015/01/05 17:00:00.105 2020.5 0
2015/01/05 17:00:01.077 2021 0.423093923
由于
答案 0 :(得分:1)
这是你在找什么?
# read in your dataframe
import pandas as pd
df = pd.read_csv('dt_data.csv', skipinitialspace=True)
df.columns = ['mydt', 'close', 'dif'] # changed your column name to 'mydt'
df.mydt = pd.to_datetime(df.mydt) # convert mydt to datetime so we can operate on it
# keep times outside [15:15 to 17:00] interval
df = df[(((df.mydt.dt.hour >= 15) & (df.mydt.dt.minute > 15))
| (df.mydt.dt.hour == 16))==False]
# increment the day count for hours >= 17 at start of new 'day'
ndx = df[df.mydt.dt.hour>=17].index
df.ix[ndx, 'mydt'] += pd.Timedelta(days=1)
df.set_index('mydt', inplace=True, drop=True)
print(df)
close dif
mydt
2015-01-02 17:00:00.211 2030.25 0.300000
2015-01-02 17:00:02.456 2030.75 0.595138
2015-01-02 00:02:01.955 2036.75 -0.400000
2015-01-02 00:04:04.887 2036.50 -0.391144
2015-01-02 15:14:56.207 2021.50 -4.732677
2015-01-02 15:14:59.020 2021.50 -4.731172
2015-01-05 17:00:00.105 2020.50 0.000000
2015-01-05 17:00:01.077 2021.00 0.423094
编辑:在评论中解决groupby问题。如果您只需要访问上面日期时间列mydt的日期部分,您可以这样做:
df.reset_index(inplace=True)
print(df.mydt.dt.date)
0 2015-01-02
1 2015-01-02
2 2015-01-02
3 2015-01-02
4 2015-01-02
5 2015-01-02
6 2015-01-05
7 2015-01-05
dtype: object
然后你可以只使用日期部分进行groupby操作
print(df.groupby(df.mydt.dt.date)['dif'].sum())
2015-01-02 -9.359855
2015-01-05 0.423094
Name: dif, dtype: float64