简体   繁体   English

将 TimedeltaIndex 转换为 DatetimeIndex

[英]Convert TimedeltaIndex to DatetimeIndex

I'm trying to extract times from a TimedeltaIndex object.我正在尝试从 TimedeltaIndex object 中提取时间。 I tried using pd.to_datetime() but got the following error:我尝试使用pd.to_datetime()但收到以下错误:

>>> import pandas as pd
>>> times = pd.timedelta_range(start='10:00:00', end='12:00:00', freq='min')
>>> pd.to_datetime(times)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../python3.6/site-packages/pandas/core/tools/datetimes.py", line 812, in to_datetime
    result = convert_listlike(arg, format, name=arg.name)
  File ".../python3.6/site-packages/pandas/core/tools/datetimes.py", line 371, in _convert_listlike_datetimes
    arg, _ = maybe_convert_dtype(arg, copy=False)
  File ".../python3.6/site-packages/pandas/core/arrays/datetimes.py", line 2123, in maybe_convert_dtype
    raise TypeError(f"dtype {data.dtype} cannot be converted to datetime64[ns]")
TypeError: dtype timedelta64[ns] cannot be converted to datetime64[ns]

I ended up using the following line but I am hoping there is a more elegant solution: (pd.to_datetime(0) + times).time我最终使用了以下行,但我希望有一个更优雅的解决方案: (pd.to_datetime(0) + times).time

You can convert TimeDeltaIndex to datetime by first converting it to integer.您可以通过首先将 TimeDeltaIndex 转换为 integer 来将其转换为日期时间。

import pandas as pd
import numpy as np

times = pd.timedelta_range(start='10:00:00', end='12:00:00', freq='min')
pd.to_datetime(times.astype(np.int64)).time

This gives the same output as the (pd.to_datetime(0) + times).time这给出了与(pd.to_datetime(0) + times).time 相同的 output

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:仅对 DatetimeIndex、TimedeltaIndex 或 PeriodIndex 有效,但得到了“RangeIndex”的实例 - TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex' pandas 不能按日期分组,仅对 DatetimeIndex、TimedeltaIndex 或 PeriodIndex 有效,但 - pandas cannot group by date, Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but Pandas重采样:TypeError:仅对DatetimeIndex,TimedeltaIndex或PeriodIndex有效,但得到&#39;RangeIndex&#39;的实例 - Pandas Resampling: TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex' 类型错误:如何修复仅对 DatetimeIndex、TimedeltaIndex 或 PeriodIndex 有效,但得到了“Index”的实例 - TypeError: How to fix Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Index' Pandas TypeError:仅对DatetimeIndex,TimedeltaIndex或PeriodIndex有效,但具有“ Int64Index”的实例 - Pandas TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Int64Index' TypeError:仅当dtype为datetime64 [ns]时对DatetimeIndex,TimedeltaIndex或PeriodIndex有效 - TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex when dtype is datetime64[ns] Pandas dataframe.resample TypeError '仅对 DatetimeIndex、TimedeltaIndex 或 PeriodIndex 有效,但获得了“RangeIndex”实例 - Pandas dataframe.resample TypeError 'Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex' 将DatetimeIndex转换为datetime - Convert DatetimeIndex to datetime Python datetime 仍然给出“TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of &#39;Index&#39;” - Python datetime still gives "TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Index'" 每半小时可以使用熊猫TimeDeltaIndex,PeriodIndex或DateTimeIndex吗? - For half-hourly intervals can I use Pandas TimeDeltaIndex, PeriodIndex or DateTimeIndex?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM