【问题标题】:Binning Pandas column of timestamps对 Pandas 时间戳列进行分箱
【发布时间】:2018-12-10 05:08:06
【问题描述】:

我正在尝试在数据框中合并一列时间戳。时间戳的格式为 0:00:00,我认为它们是字符串。我尝试使用uber.dtypes(),但它一直返回错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-b4120eada070> in <module>()
----> 1 uber.dtypes()

TypeError: 'Series' object is not callable

picture of dataframe for reference

uber["Time"].head().to_dict() 返回以下内容:

{0: '0:11:00', 1: '0:17:00', 2: '0:21:00', 3: '0:28:00', 4: '0:33:00'}

当我使用这些垃圾箱和标签时:

bins = np.arange(0, 25, 1)
labels = [
    "0:00-1:00",
    "1:01-2:00",
    "2:01-3:00",
    "3:01-4:00",
    "4:01-5:00",
    "5:01-6:00",
    "6:01-7:00",
    "7:01-8:00",
    "8:01-9:00",
    "9:01-10:00",
    "10:01-11:00",
    "11:01-12:00",
    "12:01-13:00",
    "13:01-14:00",
    "14:01-15:00",
    "15:01-16:00",
    "16:01-17:00",
    "17:01-18:00",
    "18:01-19:00",
    "19:01-20:00",
    "20:01-21:00",
    "21:01-22:00",
    "22:01-23:00",
    "23:01-24:00"
]

uber["Hour"] = pd.cut(uber["Time"], bins, labels = labels)

我收到以下错误:

TypeError: '<' not supported between instances of 'int' and 'str'

如果我将垃圾箱更改为:

bins = str(np.arange(0, 25, 1)

我收到此错误:

AxisError: axis -1 is out of bounds for array of dimension 0

我意识到我可能可以将这些转换为秒,然后使用 pd.to_numeric() 将列转换为整数,以便将它们分箱,但我已经浏览了文档,但仍不清楚如何使用日期时间或时间(我可以做很长的路并乘以秒和分钟)。

1) 如何使用日期时间或时间将这些时间戳转换为秒?

2) 有没有一种方法可以在不将时间戳转换为秒的情况下将它们分箱?

我还尝试将 uber["Time"] 中的值转换为 datetime.time 对象,并在合并之前将它们插入新列 ["Time Object"]:

for i in range(len(uber["Time"])):
    uber.loc[i, "Time Object"] = datetime.datetime.strptime(uber.loc[i, "Time"], "%H:%M:%S").time()

如果我尝试使用 ["Time Object"] 列进行分类:

uber["Hour"] = pd.cut(uber["Time Object"], bins = 24, labels = labels)

然后我收到此错误:

TypeError: '<=' not supported between instances of 'datetime.time' and 'str'

如果我尝试使用 ["Time Object"] 列的小时进行分类:

uber["Hour"] = pd.cut(uber["Time Object"].hour, bins = 24, labels = labels)

我收到此错误:

AttributeError: 'Series' object has no attribute 'hour'

【问题讨论】:

  • 应该有更好的方法。您如何uber["Time"].head().to_dict() 并将您的输出粘贴到此处?这会很有帮助。
  • 改用 df.dtypes
  • 我粘贴了 uber["Time"].head().to_dict() 输出以及我尝试 df.dtypes() 时的输出
  • 不带圆括号的dtypes

标签: python pandas time binning


【解决方案1】:

你可以试着花几分钟时间去收拾它

uber = pd.DataFrame()

labels = [str(i)+':01-'+str(i+1)+':00' for i in range(59)]    
uber['Time'] = {0: '0:11:00', 1: '0:17:00', 2: '0:21:00', 3: '0:28:00', 4: '0:33:00'}.values()
uber.Time = pd.to_timedelta(uber.Time)
pd.cut(uber.Time.dt.seconds/60,bins,labels=labels)

输出:

0    10:01-11:00
1    16:01-17:00
2    20:01-21:00
3    27:01-28:00
4    32:01-33:00
Name: Time, dtype: category
Categories (59, object): [0:01-1:00 < 1:01-2:00 < 2:01-3:00 < 3:01-4:00 ... 55:01-56:00 < 56:01-57:00 < 57:01-58:00 < 58:01-59:00]

【讨论】:

  • 你能解释一下你所说的“试着花几个小时和垃圾箱”是什么意思吗?你的意思是:在这种情况下,我的数据框是 uber,所以我会使用 uber.time.datetime.hour 并使用我在原始问题中粘贴的 bin 进行剪切?
  • uber["Hour"] = pd.cut(uber["Time"], bins = 24, labels = labels) 返回TypeError: must be str, not floatuber["Hour"] = pd.cut(uber["Time"].datetime.hour, bins = 24, labels = labels) 返回AttributeError: 'Series' object has no attribute 'datetime'。我还尝试将 ["Time"] 列转换为 datetime 对象,当我尝试 bin 时,我收到以下信息:TypeError: '&lt;=' not supported between instances of 'datetime.time' and 'str'
  • 嗨@Christina,我刚刚根据您的输入编辑了我的回复
猜你喜欢
  • 2019-03-15
  • 1970-01-01
  • 2017-12-29
  • 1970-01-01
  • 1970-01-01
  • 2018-10-19
  • 1970-01-01
相关资源
最近更新 更多