Python Datetime数据类型是对象而不是Datetime

Python Datetime数据类型是对象而不是Datetime,python,pandas,datetime,pandas-groupby,python-datetime,Python,Pandas,Datetime,Pandas Groupby,Python Datetime,我想说的是时间戳。首先,我必须将我得到的时间(字符串)转换为日期时间。转换为datetime后,我注意到,尽管指定了pandas添加日期的特定格式,但我不需要日期。我正在努力删除这个,只保留时间对象,但我没有成功。我删除日期所做的任何操作都会将数据类型返回到一个对象,而我无法在该对象上预执行groupby 示例数据: https://miratrix.co.uk/ 00:01:55 https://miratrix.co.uk/ 00:02:02 https:

我想说的是时间戳。首先,我必须将我得到的时间(字符串)转换为日期时间。转换为datetime后,我注意到,尽管指定了pandas添加日期的特定格式,但我不需要日期。我正在努力删除这个,只保留时间对象,但我没有成功。我删除日期所做的任何操作都会将数据类型返回到一个对象,而我无法在该对象上预执行groupby

示例数据:

https://miratrix.co.uk/          00:01:55
https://miratrix.co.uk/          00:02:02
https://miratrix.co.uk/          00:02:45
https://miratrix.co.uk/          00:01:22
https://miratrix.co.uk/          00:02:02
https://miratrix.co.uk/app-marketing-agency/          00:02:23
https://miratrix.co.uk/get-in-touch/          00:02:26
https://miratrix.co.uk/get-in-touch/          00:00:18
https://miratrix.co.uk/get-in-touch/          00:02:37
https://miratrix.co.uk/          00:00:31
https://miratrix.co.uk/          00:02:00
https://miratrix.co.uk/app-store-optimization-...          00:02:25
https://miratrix.co.uk/          00:03:36
https://miratrix.co.uk/app-marketing-agency/          00:02:09
https://miratrix.co.uk/get-in-touch/          00:02:14
https://?page_id=16198/          00:00:15
https://videos/channel/UCAQfRNzXGD4BQICkO1KQZUA/          00:09:07
https://miratrix.co.uk/get-in-touch/          00:01:39
https://miratrix.co.uk/app-marketing-agency/          00:01:07
我到目前为止所做的尝试

*Returned Object*
ga_organic['NEW Avg. Time on Page'] = pd.to_datetime(ga_organic['Avg. Time on Page'], format="%H:%M:%S").dt.time

*Returned Datetime but when trying to sample only time it returned an object*
ga_organic['NEW Avg. Time on Page'] = ga_organic['Avg. Time on Page'].astype('datetime64[ns]')

ga_organic['NEW Avg. Time on Page'].dt.time
我感觉datetime有些东西我不知道,这就是为什么我有这个问题。欢迎任何帮助或指导

#####更新####

感谢Alolz为时间戳提供解决方案

ga_organic['NEW Avg. Time on Page'] = pd.to_timedelta(ga_organic['Avg. Time on Page'])
但是,使用GroupBy方法时,我仍然会遇到相同的错误:

avg_time = ga_organic.groupby(ga_organic.index)['NEW Avg. Time on Page'].mean()
错误:“DataError:没有要聚合的数字类型”


是否有处理分组日期时间的特定函数?

似乎
groupby
不将
timedelta64
识别为数字类型。有几种解决方法,要么使用
numeric\u only=False
,要么使用
total\u seconds

import pandas as pd

#df = pd.read_clipboard(header=None)
#df[1] = pd.to_timedelta(df[1])

df.groupby(df.index//2)[1].mean()
#DataError: No numeric types to aggregate

# To fix pass `numeric_only=False`
df.groupby(df.index//2)[1].mean(numeric_only=False)
#0   00:01:58.500000
#1   00:02:03.500000
#2   00:02:12.500000
#3          00:01:22
#4          00:01:34
#5   00:02:12.500000
#6   00:02:52.500000
#7   00:01:14.500000
#8          00:05:23
#9          00:01:07
#Name: 1, dtype: timedelta64[ns]

将简单的
浮点值与
一起使用。总秒数

df[1] = df[1].dt.total_seconds()

df.groupby(df.index//2)[1].mean()
#0    118.5
#1    123.5
#2    132.5
#3     82.0
#4     94.0
#5    132.5
#6    172.5
#7     74.5
#8    323.0
#9     67.0
#Name: 1, dtype: float64

这可以通过指定
unit='s'

pd.转换回\u timedelta
如果您没有
日期
,那么合适的数据类型是
timedelta64
pd.转换回\u timedelta
,看起来很有效!非常感谢。但是,仍然无法使用groupby().mean()。对此有什么想法吗?您通常不会在datetime列上使用
groupby
。您可能想使用,因此可以指定执行
mean
操作的采样时间。请注意,
resample
需要日期时间索引(所以你想把
“'Avg.Time on Page'
列设置为indexI,得到一个意外的输出。时间改变了,但它实际上没有对索引进行分组。00:01:42 00:01:42 00:01:00:01:02:04.333333 00:01:30.666666 00:01:42.66666666hmm重复项似乎都有相同的值。有感觉吗需要做一个删除重复。想法?我只是随机分组以供说明。不确定您真正需要分组的是什么。数据收集不正确。我可以修复它,但为了在页面估计等方面提供适当的时间。我必须分组并平均时间。感谢您的帮助。它解决了我的问题:)