问题描述
Need Statement:我已经从 SQLite 数据库中执行了 cursor.fetchall 选择,返回了 'id' 和 'date_time',后者是文本。我想使用 pd.to_date of year、dayOfWeek、dayOfYear、hourOfDay
创建额外的列问题:按照 no-loop column add and population approach 的示例,我尝试了多种调用组合,但均无效。
我首先测试了一系列调用以确认我可以正确拆分测试日期;
sr = pd.Series(['2015-02-08 20:00:00'])
sr = pd.to_datetime(sr)
#Year: Series.dt.year The year of the datetime
#Day of week: Series.dt.dayofweek The day of the week with Monday=0,Sunday=6
#Day of year: Series.dt.dayofyear The ordinal day of the year
#Hour: Series.dt.hour The hours of the datetime
print(sr)
print(sr.dt.year )
print(sr.dt.dayofweek )
print(sr.dt.dayofyear )
print(sr.dt.hour )
一切都按预期进行;
0 2015-02-08 20:00:00
数据类型:datetime64[ns]
0 2015
数据类型:int64
0 6
数据类型:int64
0 39
数据类型:int64
0 20
数据类型:int64
我尝试过的代码通过下面的行完美运行,返回 105,861 行 x 2 列;
def splitDateTime():
try:
sqliteConnection = sqlite3.connect('TestElecConsump.db')
cursor = sqliteConnection.cursor()
print("Connected to SQLite")
sqlite_select_query = """SELECT id,date_time from WeatherRecord;"""
cursor.execute(sqlite_select_query)
records = cursor.fetchall()
print("Total rows are: ",len(records))
print("Printing first row:",records[0])
splitDatepd = pd.DataFrame(records,columns=['id','date_time'])
print("Dataframe shape:",splitDatepd.shape)
print("Dataframe : ",splitDatepd,sep='\n')
print ('records: ' + str(type(records)))
print ('splitDatepd: ' + str(type(splitDatepd)))
然而,接下来的几行没有任何输出;
#Add new column of Pandas datetime year
splitDatepd["pd-datetime"] = splitDatepd.to-datetime["date_time"].dt.year
print("Dataframe shape:",splitDatepd.shape)
print("Dataframe : ",sep='\n')
所以我决定通过省略 .year 解析重复上述操作来简化问题;
splitDatepd["pd-datetime"] = splitDatepd.to-datetime["date_time"]
splitDatepd 仍然没有变化。
当 def 完成并返回数据帧时,它的打印输出看起来与 Select 语句中的原始数据帧完全一样。
我做错了什么?
解决方法
您可以尝试在单列中使用 pd.to_datetime
函数,例如:
splitDatepd["pd_datetime"] = pd.to_datetime(splitDatepd["date_time"])
PS:记住函数名使用下码,我的意思是,它是 pd.to_datetime
而不是 pd.to-datetime
。