【问题标题】:python - Datetime calculation between two columns in python pandaspython - python pandas中两列之间的日期时间计算
【发布时间】:2016-09-05 23:19:52
【问题描述】:

我有一个这样的 DataFrame: 而这个DataFrame被称为df_NoMissing_IDV

NoDemande   NoUsager  Sens  IdVehiculeUtilise  Fait  HeureArriveeSurSite   HeureEffective      Periods
42196000013  000001    +         287Véh          1  11/07/2015 08:02:07  11/07/2015 08:02:13    Matin
42196000013  000001    -         287Véh          1  11/07/2015 08:17:09  11/07/2015 08:17:13    Matin
42196000002  000314    +         263Véh          1  11/07/2015 09:37:43  11/07/2015 09:53:37    Matin
42196000016  002372    +         287Véh          1  11/07/2015 09:46:42  11/07/2015 10:01:39    Matin
42196000015  000466    +         287Véh          1  11/07/2015 09:46:42  11/07/2015 10:01:39    Matin
42196000002  000314    -         263Véh          1  11/07/2015 10:25:17  11/07/2015 10:38:11    Matin
42196000015  000466    -         287Véh          1  11/07/2015 10:48:51  11/07/2015 10:51:30    Matin
42196000016  002372    -         287Véh          1  11/07/2015 11:40:56  11/07/2015 11:41:01    Matin
42196000004  002641    +         263Véh          1  11/07/2015 13:39:29  11/07/2015 13:52:50    Soir
42196000004  002641    -         263Véh          1  11/07/2015 13:59:56  11/07/2015 14:07:41    Soir  

我需要得到HeureArriveeSurSiteHeureEffective 列之间的差值,它们已经是datetime.datetime() 数据。

这是一个新的DataFrame,称为df1

df1 = df_NoMissing_IDV[(df_NoMissing_IDV['Sens'] == '+') & (df_NoMissing_IDV['Periods'] == 'Matin')]

df1 看起来像这样:

NoDemande   NoUsager  Sens  IdVehiculeUtilise  Fait  HeureArriveeSurSite   HeureEffective      Periods
42196000013  000001    +         287Véh          1  11/07/2015 08:02:07  11/07/2015 08:02:13    Matin
42196000002  000314    +         263Véh          1  11/07/2015 09:37:43  11/07/2015 09:53:37    Matin
42196000016  002372    +         287Véh          1  11/07/2015 09:46:42  11/07/2015 10:01:39    Matin
42196000015  000466    +         287Véh          1  11/07/2015 09:46:42  11/07/2015 10:01:39    Matin

由于它们都是datetime.datetime()数据,我尝试直接使用:

df_NoMissing_IDV['DureeService'] = df1['HeureEffective']-df1['HeureArriveeSurSite']

但它返回了TypeError: unsupported operand type(s) for -: 'unicode' and 'unicode'

而且我还尝试用datetime.time()类型进行计算,结果返回TypeError: unsupported operand type(s) for -: 'datetime.time' and 'datetime.time',我该怎么办?

编辑

我将df1 中的列转换为datetime()

df1.HeureArriveeSurSite = pd.to_datetime(df1.HeureArriveeSurSite)
df1.HeureEffective = pd.to_datetime(df1.HeureEffective)

但是下一步还是出错返回:ValueError: cannot reindex from a duplicate axis

如果我将df_NoMissing_IDV 中的列转换为datetime()

df_NoMissing_IDV.HeureArriveeSurSite = pd.to_datetime(df_NoMissing_IDV.HeureArriveeSurSite)
df_NoMissing_IDV.HeureEffective = pd.to_datetime(df_NoMissing_IDV.HeureEffective)

同样的问题仍然存在。

任何帮助将不胜感激~

【问题讨论】:

  • 我认为您需要将列转换为 datetime - df_NoMissing_IDV.HeureArriveeSurSite = pd.to_datetime(df_NoMissing_IDV.HeureArriveeSurSite) df_NoMissing_IDV.HeureEffective = pd.to_datetime(df_NoMissing_IDV.HeureEffective)
  • 您的意思是将DataFrame =df1 中的列转换为datetime?因为df_NoMissing_IDV 中的列已经在datetime 中。但是下一步仍然是错误的。 df_NoMissing_IDV['DureeService'] = df1['HeureEffective']-df1['HeureArriveeSurSite'],它返回:ValueError: cannot reindex from a duplicate axis
  • 嗯。有一些重复。你能添加样本,返回错误吗?
  • 我尝试创建一些没有成功的测试数据,但也许很容易。

标签: python datetime pandas dataframe


【解决方案1】:

我认为错误的原因是您的数据中有一些重复项。

尝试两件事:

df_NoMissing_IDV['DureeService'] = df1['HeureEffective'].values -df1['HeureArriveeSurSite'].values

或者:

df1 = df1.reset_index()

编辑: 你也可以试试timedelta:

>>> import datetime
>>> time_difference = df1['HeureEffective']-df1['HeureArriveeSurSite']
>>> time_difference_in_seconds = time_difference / timedelta(seconds=1)

【讨论】:

  • 第一个返回:ValueError: Length of values does not match length of index。第二个返回:ValueError: cannot insert level_0, already exists
  • 试试这个:df1 = df1.reset_index(drop=True)
  • 成功了。而且这个专栏加到DataFramedf1,应该是这样吗?
  • 你的意思是你现在有“索引”列?
  • 不,我在 DataFrame df1 中有列 DureeService。我还有两列indexlevel_0df1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-14
  • 2019-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-09
  • 2017-12-27
相关资源
最近更新 更多