我在python中使用panda dataframe在MySQL/MariaDB数据库中读取和存储数据时遇到了一些错误。我尝试以YYYY-MM-DD的形式将计算出的日期存储到数据帧中。我将它输入到一个数据帧中,但由于某种原因,它被转换为格式为"YYYY-MM-DD HH:MM:SS“的时间戳。
我不会显示完整的代码,但只显示与此日期错误有关的部分,因为我已经确认其他变量正在正确传递。
for j in range(0, final_day):
DateW = DateP + dt.timedelta(days = j + int(First_day))
DateWCorr=datetime.date(DateW.year,DateW.month,DateW.day)
print(DateWCorr,'New')
if j < length + offset:
if DateW != ws_date[indexw[j-offset]]:
doy = (DateW - datetime.date(today.year, 1, 1)).days + 1
new_record = pd.DataFrame(DateWCorr,columns=['Date'])
WeatherArray = pd.concat([WeatherArray,new_record])
else:
*****OMITTED FOR CLARITY SAKE**********
print("Check",WeatherArray['Date'])
WeatherArray = WeatherArray.sort_values('Date')
WeatherArray = WeatherArray.reset_index()
显示了print语句的语法:
2018-05-06 New
2018-05-07 New
2018-05-08 New
2018-05-09 New
2018-05-10 New
..............
打印WeatherArray的日期.....
2018-04-20 00:00:00
2018-04-21 00:00:00
2018-04-22 00:00:00
2018-04-23 00:00:00
.............(loops through all dates, omitted for clarity)
为什么它要将这些文件转换为不同的格式?我无论如何也想不出这件事。
发布于 2018-07-17 03:35:23
此脚本是否从字段类型为datetime
的数据库表中提取
如果是这样的话,我猜来自数据库的datetime正在与date合并,而Python正在悄悄地将date强制为datetime。
您可以尝试将数据库架构更改为date
类型,或者在将新记录放入WeatherArray
之前显式地将加载的数据更改为日期。
https://stackoverflow.com/questions/51368869
复制相似问题