【问题标题】:Assign time element of DateTimeIndex to new column将 DateTimeIndex 的时间元素分配给新列
【发布时间】:2016-10-01 01:37:06
【问题描述】:

我正在使用下面的DataFrame 并且只想访问我的DateTimeIndex 的时间(而不是日期):

                    idle wheel  Induce wheel axial  radial
tiempo          
5/30/2016 19:37:46  -1,099.12   -1,048.78   -477.13
5/30/2016 19:37:47  -1,099.12   -1,048.78   -476.98
5/30/2016 19:37:48  -1,099.12   -1,048.78   -477.21
5/30/2016 19:37:49  -1,099.12   -1,048.78   -477.13
5/30/2016 19:37:50  -1,099.12   -1,048.78   -477.21
5/30/2016 19:37:51  -1,099.12   -1,048.78   -477.35
5/30/2016 19:37:52  -1,099.12   -1,048.78   -477.13
5/30/2016 19:37:53  -1,099.12   -1,048.78   -476.98
5/30/2016 19:37:54  -1,099.12   -1,048.78   -476.98
5/30/2016 19:37:55  -1,099.12   -1,048.78   -476.98
5/30/2016 19:37:56  -1,099.12   -1,048.78   -476.98
5/30/2016 19:37:57  -1,099.12   -1,048.78   -476.98

我只想保留19:..:..,而不是日期部分。 我一直在寻找,但找不到任何解决方案。

【问题讨论】:

标签: python pandas dataframe datetimeindex


【解决方案1】:

在您的 index 'tiempo' 上使用 .time

df['time'] = df.index.time

或者,如果 'tiempo'column 使用

df['time'] = df.tiempo.dt.time

得到:

                tiempo  idle wheel  Induce wheel axial    radial      time
0  2016-05-30 19:37:46    -1099.12    -1048.78           -477.13  19:37:46
1  2016-05-30 19:37:47    -1099.12    -1048.78           -476.98  19:37:47
2  2016-05-30 19:37:48    -1099.12    -1048.78           -477.21  19:37:48
3  2016-05-30 19:37:49    -1099.12    -1048.78           -477.13  19:37:49
4  2016-05-30 19:37:50    -1099.12    -1048.78           -477.21  19:37:50
5  2016-05-30 19:37:51    -1099.12    -1048.78           -477.35  19:37:51
6  2016-05-30 19:37:52    -1099.12    -1048.78           -477.13  19:37:52
7  2016-05-30 19:37:53    -1099.12    -1048.78           -476.98  19:37:53
8  2016-05-30 19:37:54    -1099.12    -1048.78           -476.98  19:37:54
9  2016-05-30 19:37:55    -1099.12    -1048.78           -476.98  19:37:55
10 2016-05-30 19:37:56    -1099.12    -1048.78           -476.98  19:37:56
11 2016-05-30 19:37:57    -1099.12    -1048.78           -476.98  19:37:57

【讨论】:

  • 出于兴趣,您是如何设法复制 OP 的 DataFrame 以在 Python 中使用它的,您是如何将格式良好的文本发布到答案中的?我经常在 df 相关问题中遇到最小的工作示例......
  • 查看pd.read_clipboard() 以及使用string.IOpd.read_csv() 的示例。在这里,我实际上只是复制到excel,我认为column 名称实际上存在错误,现在我正在查看它。其余的只是使用 SO 格式选项({}
  • 哪个命令,错误是什么?你的Index 已经是DateTimeIndex 格式了吗?
猜你喜欢
  • 2019-05-09
  • 1970-01-01
  • 1970-01-01
  • 2019-01-14
  • 2020-06-09
  • 2012-01-05
  • 2023-03-16
  • 2019-08-31
  • 2016-04-21
相关资源
最近更新 更多