产生此错误:TypeError:'NoneType'对象不可下标

时间:2018-12-10 20:08:39

标签: python pandas numpy

当我尝试读取文件时,我一直收到此错误。我该如何解决?

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
from sklearn.cluster import KMeans


MY_FILE = 'prokaryotes.csv'

df = pd.read_csv("prokaryotes.csv", parse_dates=[0], header=None, 
names=['datetime', 'consumption'])

df['date'] = [x.date() for x in df['datetime']]
df['time'] = [x.time() for x in df['datetime']]



TypeError: 'NoneType' object is not subscriptable

1 个答案:

答案 0 :(得分:0)

您可以尝试以此替换代码的最后两行:

df['date'] = df.datetime.apply(lambda s : s[:10])
df['time'] = df.datetime.apply(lambda s : s[-9:])
print(df)