pandas.DataFrame.to_timestamp() 不支持的类型 RangeIndex [重复]
python 419
原文标题 :unsupported Type RangeIndex for pandas.DataFrame.to_timestamp() [duplicate]
我有一个以字符串格式导入的 pandas DataFrame 中的字段。它应该是一个日期时间变量。如何将其转换为日期时间列,然后根据日期进行过滤。
例子:
- 数据帧名称:raw_data
- 栏目名称:Mycol
- 列中的值格式:’05SEP2014:00:00:00.000′
回复
我来回复-
chrisb 评论
该回答已被采纳!
使用
to_datetime
函数,指定格式以匹配您的数据。raw_data['Mycol'] = pd.to_datetime(raw_data['Mycol'], format='%d%b%Y:%H:%M:%S.%f')
2年前 -
Vlad Bezden 评论
如果您有多个要转换的列,您可以执行以下操作:
df[["col1", "col2", "col3"]] = df[["col1", "col2", "col3"]].apply(pd.to_datetime)
2年前 -
mechanical_meat 评论
您可以使用DataFrame方法
.apply()
对Mycol中的值进行操作:>>> df = pd.DataFrame(['05SEP2014:00:00:00.000'],columns=['Mycol']) >>> df Mycol 0 05SEP2014:00:00:00.000 >>> import datetime as dt >>> df['Mycol'] = df['Mycol'].apply(lambda x: dt.datetime.strptime(x,'%d%b%Y:%H:%M:%S.%f')) >>> df Mycol 0 2014-09-05
2年前 -
Prateek Sharma 评论
使用 pandas
to_datetime
函数将列解析为 DateTime。此外,通过使用infer_datetime_format=True
,它会自动检测格式并将提到的列转换为日期时间。import pandas as pd raw_data['Mycol'] = pd.to_datetime(raw_data['Mycol'], infer_datetime_format=True)
2年前 -
Darth BEHFANS 评论
raw_data['Mycol'] = pd.to_datetime(raw_data['Mycol'], format='%d%b%Y:%H:%M:%S.%f')
有效,但它会导致 Python 警告 A value is trying to be set on a copy of a slice from DataFrame.Try using
.loc[row_indexer,col_indexer] = value
instead我猜这是由于一些链接索引。
2年前 -
Gil Baggio 评论
省时间:
raw_data['Mycol'] = pd.to_datetime(raw_data['Mycol'])
2年前 -
hotplasma 评论
需要注意的是,pandas.to_datetime 几乎永远不会返回 datetime.datetime。来自文档
块引用
Returns datetime If parsing succeeded. Return type depends on input: list-like: DatetimeIndex Series: Series of datetime64 dtype scalar: Timestamp In case when it is not possible to return designated types (e.g. when any element of input is before Timestamp.min or after Timestamp.max) return will have datetime.datetime type (or corresponding array/Series).
块引用
2年前