【问题标题】:Joining multiple DateTime columns into one Columns (Python)将多个 DateTime 列合并为一个列(Python)
【发布时间】:2021-07-09 05:33:32
【问题描述】:

我试图将我所有的日期时间信息收集到一个列中。现在我在 6 个月的跨度内的每个时期都有一个列,但是为了进行时间序列分析,我试图将所有日期时间收集到一个列中。

这就是我累的地方:

 df['Dates'] = df["1 Month Date"]+ df["2 Month Date"]+ df["3 Month Date"] + df["4 Month Date"] + df["5 Month Date"] +df["6 Month Date"]

错误信息:

  TypeError: cannot add DatetimeArray and DatetimeArray

二审:

Dates = df["1 Month Date","2 Month Date","3 Month Date","4 Month Date","5 Month Date","6 Month Date"]

错误信息:

KeyError: ('1 Month Date', '2 Month Date', '3 Month Date', '4 Month Date', '5 Month Date', '6 Month Date')

额外信息:这是我使用 panda 导入的 excel 表,当我执行 df.info()) 时,我的 1 Month date 2 Month date ect .. 是 datetime64[ns]

样本数据:

   1 Month Date  1 Month Room Booked 2 Month Date  2 Month Room Booked  \
0    2020-09-01                  339   2020-10-01                  346   
1    2020-09-01                    2   2020-10-01                    4   
2    2020-09-01                    4   2020-10-01                    4   
3    2020-09-01                    0   2020-10-01                    0   
4    2020-09-01                    0   2020-10-01                    0   
5    2020-09-01                    1   2020-10-01                    1   
6    2020-09-01                    2   2020-10-01                    2   
7    2020-09-01                   50   2020-10-01                   58   
8    2020-09-01                   12   2020-10-01                   12   
9    2020-09-01                    9   2020-10-01                    9   
10   2020-09-01                    6   2020-10-01                    6   
11   2021-03-01                  112   2021-04-01                  112   
12   2021-03-01                    0   2021-04-01                    0   
13   2021-02-01                   36   2021-03-01                   36   
14   2021-02-01                   18   2021-03-01                   18   
15   2021-02-01                   20   2021-03-01                   20   
16   2021-02-01                   12   2021-03-01                   12   
17   2021-02-01                    0   2021-03-01                    0   

【问题讨论】:

  • print (df.info()) 是什么?你也使用最后一个熊猫版本吗?
  • 嘿,感谢您的评论,df.info 告诉您您拥有什么类型的数据,所以我的数据是 datetime64[ns],我认为是这样,它如何影响我正在尝试做的事情?如何检查?
  • 预期输出应该是 2 列?
  • 没有 6 列,例如 1 Month Date , 2 Month date until 6 ,我试图将所有这 6 个月的日期收集到所有日期的 1 列中

标签: python pandas datetime typeerror keyerror


【解决方案1】:

IIUC 使用:

Dates = df[["1 Month Date","2 Month Date","3 Month Date","4 Month Date","5 Month Date","6 Month Date"]].apply(pd.Series.explode).sum(axis=1)

【讨论】:

  • 但是当我做 print(df.Dates) 它说: AttributeError: 'DataFrame' object has no attribute 'Dates'
  • @XIUJY1111 - 您可以创建小数据样本来查看数据的外观吗?例如3 行 3 列。
  • 当我打印(日期)时,它向我显示了这个(如下)我不能用于我的时间序列分析 0 0.0 1 0.0 2 0.0 3 0.0 4 0.0 5 0.0 6 0.0 7 0.0 8 0.0 9 0.0 10 0.0 11 0.0 12 0.0 13 0.0 14 0.0 15 0.0 16 0.0 17 0.0 18 0.0 19 0.0 20 0.0 21 0.0 22 0.0 23 0.0 24 0.0 告诉我你的想法
  • @XIUJY1111 - 老实说,如果错误TypeError: cannot add DatetimeArray and DatetimeArray 似乎是一些列表数据,但我需要具有相同错误的小数据样本。是否可以创建它并添加到问题中?
  • 嘿,我添加了一些数据示例
猜你喜欢
  • 1970-01-01
  • 2016-01-10
  • 1970-01-01
  • 1970-01-01
  • 2018-07-05
  • 2014-05-16
  • 2022-01-07
  • 1970-01-01
相关资源
最近更新 更多