pd DataFrame,需要添加列,一次将文本字符串date_time解析为pandas年,dayOfWeek等
pd DataFrame, need to add columns, parsing text string date_time into pandas year, dayOfWeek, etc in one pass
需要语句:我已经从 SQLite 数据库执行了 cursor.fetchall Select,返回 'id' 和 'date_time',后者是文本。我想使用 pd.to_date of year、dayOfWeek、dayOfYear、hourOfDay
创建其他列
问题: 按照 no-loop column add and population approach 的示例,我尝试了多个调用组合,其中 none 有效。
我首先测试了一系列调用以确认我可以正确拆分测试日期;
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
dtype: datetime64[ns]
0 2015
dtype: int64
0 6
dtype: int64
0 39
dtype: int64
0 20
dtype: 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 : " , splitDatepd, sep='\n')
所以我决定通过省略 .year 解析来重复上述操作来简化问题;
splitDatepd["pd-datetime"] = splitDatepd.to-datetime["date_time"]
splitDatepd 仍然没有变化。
当 def 完成并 returns 数据帧时,它的打印输出看起来与 Select 语句中的原始数据帧完全一样。
我做错了什么?
您可以尝试在单个列中使用 pd.to_datetime
函数,例如:
splitDatepd["pd_datetime"] = pd.to_datetime(splitDatepd["date_time"])
PS:记住函数名使用下划线,我的意思是,它是 pd.to_datetime
而不是 pd.to-datetime
。
需要语句:我已经从 SQLite 数据库执行了 cursor.fetchall Select,返回 'id' 和 'date_time',后者是文本。我想使用 pd.to_date of year、dayOfWeek、dayOfYear、hourOfDay
创建其他列问题: 按照 no-loop column add and population approach 的示例,我尝试了多个调用组合,其中 none 有效。
我首先测试了一系列调用以确认我可以正确拆分测试日期;
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
dtype: datetime64[ns]
0 2015 dtype: int64
0 6
dtype: int64
0 39
dtype: int64
0 20
dtype: 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 : " , splitDatepd, sep='\n')
所以我决定通过省略 .year 解析来重复上述操作来简化问题;
splitDatepd["pd-datetime"] = splitDatepd.to-datetime["date_time"]
splitDatepd 仍然没有变化。
当 def 完成并 returns 数据帧时,它的打印输出看起来与 Select 语句中的原始数据帧完全一样。
我做错了什么?
您可以尝试在单个列中使用 pd.to_datetime
函数,例如:
splitDatepd["pd_datetime"] = pd.to_datetime(splitDatepd["date_time"])
PS:记住函数名使用下划线,我的意思是,它是 pd.to_datetime
而不是 pd.to-datetime
。