Python: How can I drop the first 5 minutes of each day in my time serie?
up vote
3
down vote
favorite
I have a dataframe with columns: Date of a transaction , Time of the transaction and Price. I want to drop the first and last 5 minutes in each day.
Here is an example:
----------------------------------------
Date | Time | Price
----------------------------------------
03/03/2014 | 09:36:36.814 | 43.90
---------------------------------------
03/03/2014 | 09:37:02.381 | 43.40
---------------------------------------
03/03/2014 | 09:41:02.381 | 43.40
---------------------------------------
03/03/2014 | 09:50:02.381 | 43.40
---------------------------------------
I want to get this output:
----------------------------------------
Date | Time | Price
---------------------------------------
03/03/2014 | 09:50:02.381 | 43.40
---------------------------------------
I need to do this for each day of time serie.
I tried this code:
trades14081.insert(2,'DateTime',pd.to_datetime(trades14081['Date']+trades14081['Time'], format = "%d/%m/%Y%H:%M:%S.%f" ))
delta=datetime.timedelta(minutes=5)
i=0
j=0
start=
end=
while trades14081['Date'][i]==trades14081['Date'][j] and j<len(trades14081):
if trades14081['DateTime'][j]-trades14081['DateTime'][i]<delta:
j=j+1
else:
start.append(i)
end.append(j)
j=j+1
while trades14081['Date'][i]==trades14081['Date'][j] and j<len(trades14081):
j=j+1
i=j
for i in range(len(start)):
trades14081=trades14081.drop(trades14081.index[start[i]:end[i]])
But I keep on getting this error:
KeyError: 19996
> 12 l.append(j)
> 13 j=j+1
> ---> 14 while trades14081['Date'][i]==trades14081['Date'][j]:
> 15 j=j+1
> 16 i=j
19996 is the length of my dataframe trades14081.
Any ideas?
python python-3.x pandas pandas-groupby timedelta
add a comment |
up vote
3
down vote
favorite
I have a dataframe with columns: Date of a transaction , Time of the transaction and Price. I want to drop the first and last 5 minutes in each day.
Here is an example:
----------------------------------------
Date | Time | Price
----------------------------------------
03/03/2014 | 09:36:36.814 | 43.90
---------------------------------------
03/03/2014 | 09:37:02.381 | 43.40
---------------------------------------
03/03/2014 | 09:41:02.381 | 43.40
---------------------------------------
03/03/2014 | 09:50:02.381 | 43.40
---------------------------------------
I want to get this output:
----------------------------------------
Date | Time | Price
---------------------------------------
03/03/2014 | 09:50:02.381 | 43.40
---------------------------------------
I need to do this for each day of time serie.
I tried this code:
trades14081.insert(2,'DateTime',pd.to_datetime(trades14081['Date']+trades14081['Time'], format = "%d/%m/%Y%H:%M:%S.%f" ))
delta=datetime.timedelta(minutes=5)
i=0
j=0
start=
end=
while trades14081['Date'][i]==trades14081['Date'][j] and j<len(trades14081):
if trades14081['DateTime'][j]-trades14081['DateTime'][i]<delta:
j=j+1
else:
start.append(i)
end.append(j)
j=j+1
while trades14081['Date'][i]==trades14081['Date'][j] and j<len(trades14081):
j=j+1
i=j
for i in range(len(start)):
trades14081=trades14081.drop(trades14081.index[start[i]:end[i]])
But I keep on getting this error:
KeyError: 19996
> 12 l.append(j)
> 13 j=j+1
> ---> 14 while trades14081['Date'][i]==trades14081['Date'][j]:
> 15 j=j+1
> 16 i=j
19996 is the length of my dataframe trades14081.
Any ideas?
python python-3.x pandas pandas-groupby timedelta
Can you share sample input and expected output? That'll make it clearer for us.
– Mayank Porwal
Nov 11 at 11:00
This problem domain looks like it's more suited to something like Observables. RxJS/Marble diagrams rxmarbles.com - egghead.io and angular university have good course around this if you were coming at it from a JS perspective. Perhaps github.com/ReactiveX/RxPY would work - have never used it..
– JGFMK
Nov 11 at 11:02
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I have a dataframe with columns: Date of a transaction , Time of the transaction and Price. I want to drop the first and last 5 minutes in each day.
Here is an example:
----------------------------------------
Date | Time | Price
----------------------------------------
03/03/2014 | 09:36:36.814 | 43.90
---------------------------------------
03/03/2014 | 09:37:02.381 | 43.40
---------------------------------------
03/03/2014 | 09:41:02.381 | 43.40
---------------------------------------
03/03/2014 | 09:50:02.381 | 43.40
---------------------------------------
I want to get this output:
----------------------------------------
Date | Time | Price
---------------------------------------
03/03/2014 | 09:50:02.381 | 43.40
---------------------------------------
I need to do this for each day of time serie.
I tried this code:
trades14081.insert(2,'DateTime',pd.to_datetime(trades14081['Date']+trades14081['Time'], format = "%d/%m/%Y%H:%M:%S.%f" ))
delta=datetime.timedelta(minutes=5)
i=0
j=0
start=
end=
while trades14081['Date'][i]==trades14081['Date'][j] and j<len(trades14081):
if trades14081['DateTime'][j]-trades14081['DateTime'][i]<delta:
j=j+1
else:
start.append(i)
end.append(j)
j=j+1
while trades14081['Date'][i]==trades14081['Date'][j] and j<len(trades14081):
j=j+1
i=j
for i in range(len(start)):
trades14081=trades14081.drop(trades14081.index[start[i]:end[i]])
But I keep on getting this error:
KeyError: 19996
> 12 l.append(j)
> 13 j=j+1
> ---> 14 while trades14081['Date'][i]==trades14081['Date'][j]:
> 15 j=j+1
> 16 i=j
19996 is the length of my dataframe trades14081.
Any ideas?
python python-3.x pandas pandas-groupby timedelta
I have a dataframe with columns: Date of a transaction , Time of the transaction and Price. I want to drop the first and last 5 minutes in each day.
Here is an example:
----------------------------------------
Date | Time | Price
----------------------------------------
03/03/2014 | 09:36:36.814 | 43.90
---------------------------------------
03/03/2014 | 09:37:02.381 | 43.40
---------------------------------------
03/03/2014 | 09:41:02.381 | 43.40
---------------------------------------
03/03/2014 | 09:50:02.381 | 43.40
---------------------------------------
I want to get this output:
----------------------------------------
Date | Time | Price
---------------------------------------
03/03/2014 | 09:50:02.381 | 43.40
---------------------------------------
I need to do this for each day of time serie.
I tried this code:
trades14081.insert(2,'DateTime',pd.to_datetime(trades14081['Date']+trades14081['Time'], format = "%d/%m/%Y%H:%M:%S.%f" ))
delta=datetime.timedelta(minutes=5)
i=0
j=0
start=
end=
while trades14081['Date'][i]==trades14081['Date'][j] and j<len(trades14081):
if trades14081['DateTime'][j]-trades14081['DateTime'][i]<delta:
j=j+1
else:
start.append(i)
end.append(j)
j=j+1
while trades14081['Date'][i]==trades14081['Date'][j] and j<len(trades14081):
j=j+1
i=j
for i in range(len(start)):
trades14081=trades14081.drop(trades14081.index[start[i]:end[i]])
But I keep on getting this error:
KeyError: 19996
> 12 l.append(j)
> 13 j=j+1
> ---> 14 while trades14081['Date'][i]==trades14081['Date'][j]:
> 15 j=j+1
> 16 i=j
19996 is the length of my dataframe trades14081.
Any ideas?
python python-3.x pandas pandas-groupby timedelta
python python-3.x pandas pandas-groupby timedelta
edited Nov 11 at 12:05
jpp
85.5k194898
85.5k194898
asked Nov 11 at 10:34
Nada Baili
193
193
Can you share sample input and expected output? That'll make it clearer for us.
– Mayank Porwal
Nov 11 at 11:00
This problem domain looks like it's more suited to something like Observables. RxJS/Marble diagrams rxmarbles.com - egghead.io and angular university have good course around this if you were coming at it from a JS perspective. Perhaps github.com/ReactiveX/RxPY would work - have never used it..
– JGFMK
Nov 11 at 11:02
add a comment |
Can you share sample input and expected output? That'll make it clearer for us.
– Mayank Porwal
Nov 11 at 11:00
This problem domain looks like it's more suited to something like Observables. RxJS/Marble diagrams rxmarbles.com - egghead.io and angular university have good course around this if you were coming at it from a JS perspective. Perhaps github.com/ReactiveX/RxPY would work - have never used it..
– JGFMK
Nov 11 at 11:02
Can you share sample input and expected output? That'll make it clearer for us.
– Mayank Porwal
Nov 11 at 11:00
Can you share sample input and expected output? That'll make it clearer for us.
– Mayank Porwal
Nov 11 at 11:00
This problem domain looks like it's more suited to something like Observables. RxJS/Marble diagrams rxmarbles.com - egghead.io and angular university have good course around this if you were coming at it from a JS perspective. Perhaps github.com/ReactiveX/RxPY would work - have never used it..
– JGFMK
Nov 11 at 11:02
This problem domain looks like it's more suited to something like Observables. RxJS/Marble diagrams rxmarbles.com - egghead.io and angular university have good course around this if you were coming at it from a JS perspective. Perhaps github.com/ReactiveX/RxPY would work - have never used it..
– JGFMK
Nov 11 at 11:02
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
groupby + Boolean indexing
You can and should avoid Python-level loops. Here you can use groupby:
# convert strings to timedelta
df['Time'] = pd.to_timedelta(df['Time'])
# define offset from start to omit
offset = pd.Timedelta(minutes=5)
# apply Boolean filter to dataframe
res = df.loc[df['Time'] > df.groupby('Date')['Time'].transform('min') + offset]
print(res)
Date Time Price
4 03/03/2014 09:40:00 41
5 03/03/2014 09:46:00 42
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
groupby + Boolean indexing
You can and should avoid Python-level loops. Here you can use groupby:
# convert strings to timedelta
df['Time'] = pd.to_timedelta(df['Time'])
# define offset from start to omit
offset = pd.Timedelta(minutes=5)
# apply Boolean filter to dataframe
res = df.loc[df['Time'] > df.groupby('Date')['Time'].transform('min') + offset]
print(res)
Date Time Price
4 03/03/2014 09:40:00 41
5 03/03/2014 09:46:00 42
add a comment |
up vote
2
down vote
groupby + Boolean indexing
You can and should avoid Python-level loops. Here you can use groupby:
# convert strings to timedelta
df['Time'] = pd.to_timedelta(df['Time'])
# define offset from start to omit
offset = pd.Timedelta(minutes=5)
# apply Boolean filter to dataframe
res = df.loc[df['Time'] > df.groupby('Date')['Time'].transform('min') + offset]
print(res)
Date Time Price
4 03/03/2014 09:40:00 41
5 03/03/2014 09:46:00 42
add a comment |
up vote
2
down vote
up vote
2
down vote
groupby + Boolean indexing
You can and should avoid Python-level loops. Here you can use groupby:
# convert strings to timedelta
df['Time'] = pd.to_timedelta(df['Time'])
# define offset from start to omit
offset = pd.Timedelta(minutes=5)
# apply Boolean filter to dataframe
res = df.loc[df['Time'] > df.groupby('Date')['Time'].transform('min') + offset]
print(res)
Date Time Price
4 03/03/2014 09:40:00 41
5 03/03/2014 09:46:00 42
groupby + Boolean indexing
You can and should avoid Python-level loops. Here you can use groupby:
# convert strings to timedelta
df['Time'] = pd.to_timedelta(df['Time'])
# define offset from start to omit
offset = pd.Timedelta(minutes=5)
# apply Boolean filter to dataframe
res = df.loc[df['Time'] > df.groupby('Date')['Time'].transform('min') + offset]
print(res)
Date Time Price
4 03/03/2014 09:40:00 41
5 03/03/2014 09:46:00 42
answered Nov 11 at 12:02
jpp
85.5k194898
85.5k194898
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247879%2fpython-how-can-i-drop-the-first-5-minutes-of-each-day-in-my-time-serie%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you share sample input and expected output? That'll make it clearer for us.
– Mayank Porwal
Nov 11 at 11:00
This problem domain looks like it's more suited to something like Observables. RxJS/Marble diagrams rxmarbles.com - egghead.io and angular university have good course around this if you were coming at it from a JS perspective. Perhaps github.com/ReactiveX/RxPY would work - have never used it..
– JGFMK
Nov 11 at 11:02