Is It Possible To Use Cut On A Collection Of Datetimes?
Is it possible to use pandas.cut to make bins out of datetime stamps? The following code: import pandas as pd import StringIO contenttext = '''Time,Bid 2014-03-05 21:56:05:924300,
Solution 1:
Old question, but for any future visitors, I think this is a clearer way to calculate float timedeltas to use cut on:
import pandas as pd
import datetime as dt
# Get Days Since Date
today = dt.date.today()
df['days ago'] = (today - df['time']).dt.days
# Get Seconds Since Datetime
now = dt.datetime.now()
df['seconds ago'] = (now - df['time']).dt.seconds
# Minutes Since Datetime# (no dt.minutes attribute, so we use seconds/60)
now = dt.datetime.now()
df['minutes ago'] = (now - df['times']).dt.seconds/60
All of these columns are now float values that we can use pd.cut()
on
Solution 2:
Here is my work-around. You might need to change the code slightly to suit your precision needs. I use date as an example below:
# map dates to timedelta
today=dt.date.today()
# x below is a timedelta,# use x.value below if you need more precision
df['days']=map(lambda x : x.days, df.Time - today)
pd.cut(df.days, bins=5)
Effectively you turn datetime
or date
into a numerical distance measure, then cut/qcut it.
Post a Comment for "Is It Possible To Use Cut On A Collection Of Datetimes?"