本文介绍了可以对日期时间集合使用cut吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用pandas.cut
从datetime
邮票中制成垃圾箱?
以下代码:
import pandas as pd
import StringIO
contenttext = """Time,Bid
2014-03-05 21:56:05:924300,1.37275
2014-03-05 21:56:05:924351,1.37272
2014-03-05 21:56:06:421906,1.37275
2014-03-05 21:56:06:421950,1.37272
2014-03-05 21:56:06:920539,1.37275
2014-03-05 21:56:06:920580,1.37272
2014-03-05 21:56:09:071981,1.37275
2014-03-05 21:56:09:072019,1.37272"""
content = StringIO.StringIO(contenttext)
df = pd.read_csv(content, header=0)
df['Time'] = pd.to_datetime(df['Time'], format='%Y-%m-%d %H:%M:%S:%f')
pd.cut(df['Time'], 5)
引发以下错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-f5387a84c335> in <module>()
16 df['Time'] = pd.to_datetime(df['Time'], format='%Y-%m-%d %H:%M:%S:%f')
17
---> 18 pd.cut(df['Time'], 5)
/home/???????/sites/varsite/venv/local/lib/python2.7/site-packages/pandas/tools/tile.pyc in cut(x, bins, right, labels, retbins, precision, include_lowest)
80 else:
81 rng = (nanops.nanmin(x), nanops.nanmax(x))
---> 82 mn, mx = [mi + 0.0 for mi in rng]
83
84 if mn == mx: # adjust end points before binning
TypeError: unsupported operand type(s) for +: 'Timestamp' and 'float'
解决方案
旧问题,但是对于任何未来的访问者,我认为这是计算使用cut on的float timedelta的更清晰方法:
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
所有这些列现在都是浮点值,我们可以在
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
上使用
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,1.37275
2014-03-05 21:56:05:924351,1.37272
2014-03-05 21:56:06:421906,1.37275
2014-03-05 21:56:06:421950,1.37272
2014-03-05 21:56:06:920539,1.37275
2014-03-05 21:56:06:920580,1.37272
2014-03-05 21:56:09:071981,1.37275
2014-03-05 21:56:09:072019,1.37272"""
content = StringIO.StringIO(contenttext)
df = pd.read_csv(content, header=0)
df['Time'] = pd.to_datetime(df['Time'], format='%Y-%m-%d %H:%M:%S:%f')
pd.cut(df['Time'], 5)
Throws the following error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-f5387a84c335> in <module>()
16 df['Time'] = pd.to_datetime(df['Time'], format='%Y-%m-%d %H:%M:%S:%f')
17
---> 18 pd.cut(df['Time'], 5)
/home/???????/sites/varsite/venv/local/lib/python2.7/site-packages/pandas/tools/tile.pyc in cut(x, bins, right, labels, retbins, precision, include_lowest)
80 else:
81 rng = (nanops.nanmin(x), nanops.nanmax(x))
---> 82 mn, mx = [mi + 0.0 for mi in rng]
83
84 if mn == mx: # adjust end points before binning
TypeError: unsupported operand type(s) for +: 'Timestamp' and 'float'
解决方案
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
这篇关于可以对日期时间集合使用cut吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!