我正在使用Python中的.sav (SPSS)文件。在使用PyreadStat (也是使用Pandas)时,导入后的所有变量看起来都很好,除了datetime变量之外。他们使用Python读取类型浮动的指数型数。但他们的原始SPSS格式是dd-mmm-yy (例如,02-2月-2021).
日期变量是这样的
1.383160e+10
是否有一种方法可以使用Python将此格式转换为日期时间?
我尝试过各种使用日期时间模块和时间模块的方法。但我得到的是2408年的约会
# Here I'm using the float from the first row in the dataframe
time.gmtime(13831603200)结果
time.struct_time(tm_year=2408,tm_mon=4,tm_mday=22,tm_hour=0,tm_min=0,tm_sec=0,tm_wday=1,tm_yday=113,tm_isdst=0)
当我使用datetime模块时:
python_date = datetime.fromtimestamp(13831603200).strftime('%d-%b-%Y, %H:%M:%S')
print(python_date)22-4月22日-2408,00:00:00
使用Python1:https://i.stack.imgur.com/I7yza.png时日期时间变量(Vdatesub)是如何显示的
发布于 2022-09-12 11:49:44
在这两个帖子(一个Python,一个R)下面回答了这个问题:
Convert 'seconds since October 14, 1582' to Python datetime
Read SPSS file into R, the data format for date is wrong, and generate more variable
简而言之:从1582年10月14日起,该日期存储为秒数,而Python则从Epoch日期(1970年1月1日)开始。
您需要计算1582-10-14和1970-01-01之间的秒数,以调整时间戳值,如下所示:
Timestamp out of range for platform localtime()/gmtime() function
(可能为12 218 515 200秒)
https://stackoverflow.com/questions/73688683
复制相似问题