问题“浮动”对象,无法切片?

时间:2019-06-09 23:40:57

标签: python pandas datetime

我正在尝试将包含最初显示为类型'20140701165829'的日期值的列转换为日期时间格式(在这种情况下为year = 2014,month = 07,day = 01。但是我是执行整数除法后,仍然出现“无法浮动的对象”错误。

我尝试引用'float' object is unsliceable,四舍五入,使用除法类型并仅运行以下代码块(有效):     (df.timestamp_first_active // 1000000)

为了能够进行全面和最小的测试,我的数据源来自以下方面: https://github.com/FraPochetti/Airbnb/blob/master/data/train_users_2.csv https://github.com/FraPochetti/Airbnb/blob/master/data/test_users.csv

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
train_users = pd.read_csv("C:/Users/mmcgown/Downloads/train_users_2.csv")
test_users = pd.read_csv("C:/Users/mmcgown/Downloads/test_users.csv")
df = pd.concat((train_users, test_users), axis=0, ignore_index=True)
df['timestamp_first_active'] = pd.to_datetime((df.timestamp_first_active 
// 1000000), format='%Y%m%d')

---> 11 df['timestamp_first_active'] = 
pd.to_datetime((df.timestamp_first_active // 1000000), format='%Y%m%d')


--> 451             values = _convert_listlike(arg._values, True, format)

pandas\_libs\tslibs\strptime.pyx in 
pandas._libs.tslibs.strptime.array_strptime()

TypeError: 'float' object is unsliceable

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,我改用astype(),它对我有用。我会尝试:

(df.timestamp_first_active // 1000000).astype('datetime64[ns]')