Pandas: Read Timestamp from CSV in GMT then resample

感情迁移 提交于 2020-03-06 07:35:29

问题


I have a CSV with epoch GMT timestamp at irregular intervals paired with a value. I tried reading in from the CSV but all the times are shifted to my local timezone. How do I have it just read in as-is (in GMT)? Then I would like the resample to one minute intervals, HOWEVER, I would like to be able to have it skip gaps which are larger than a user specified value. If this is not possible, is there way to resample to to one minute, but in the gaps, put in an arbitrary value like 0.0?

 Data:
 Time,Data
 1354979750250,0.2343
 1354979755250,2.3433
 1354979710250,1.2343

 def date_utc(s):
     return parse(s, tzinfos=tzutc)

 x = read_csv("time2.csv", date_parser=date_utc, converters={'Time': lambda x:datetime.fromtimestamp(int(x)/1000.)}).set_index('Time')

回答1:


Convert local datetime to GMT datetime like this:

gmtDatetime = localdatetime - datetime.timedelta(hours=8)

The time zone is +08 (China).

Or using 'datetime.utcfromtimestamp':

classmethod datetime.utcfromtimestamp(timestamp)
classmethod datetime.fromtimestamp(timestamp, tz=None)

Return the UTC datetime corresponding to the POSIX timestamp, with tzinfo None. This may raise OverflowError, if the timestamp is out of the range of values supported by the platform C gmtime() function, and OSError on gmtime() failure. It’s common for this to be restricted to years in 1970 through 2038. See also fromtimestamp().



来源:https://stackoverflow.com/questions/14371476/pandas-read-timestamp-from-csv-in-gmt-then-resample

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!