- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个时间序列文件,它们应该在 CET/CEST 中。其中不好的一个,没有以正确的方式写入值。对于好的 csv,请参见此处:
#test_good.csv
local_time,value
...
2017-03-26 00:00,2016
2017-03-26 01:00,2017
2017-03-26 03:00,2018
2017-03-26 04:00,2019
...
2017-10-29 01:00,7224
2017-10-29 02:00,7225
2017-10-29 02:00,7226
2017-10-29 03:00,7227
...
...一切正常使用:
df['utc_time'] = pd.to_datetime(df[local_time_column])
.dt.tz_localize('CET', ambiguous="infer")
.dt.tz_convert('UTC').dt.strftime('%Y-%m-%d %H:%M:%S')
将 test_bad.csv 转换为 UTC 时,我收到 AmbiguousTimeError,因为缺少 10 月的 2 小时。
# test_bad.csv
local_time,value
...
2017-03-26 00:00,2016
2017-03-26 01:00,2017 # everything is as it should be
2017-03-26 03:00,2018
2017-03-26 04:00,2019
...
2017-10-29 01:00,7223
2017-10-29 02:00,7224 # the value of 2 am should actually be repeated PLUS 3 am is missing
2017-10-29 04:00,7226
2017-10-29 05:00,7227
...
有谁知道如何将时间序列文件转换为 UTC 并为新索引中的缺失日期添加 NaN 列的优雅方法?感谢您的帮助。
最佳答案
详细阐述 Mark Ransom 的评论;
2017-10-29 02:00,7224
是模棱两可的;它可能是 2017-10-29 00:00 UTC
或 2017-10-29 01:00 UTC
.这就是为什么 pd.to_datetime 拒绝推断任何东西。
借助一些 native Python,您可以解决这个问题。假设您只是将 csv 加载到 df 而没有将任何内容解析到 datetime,您可以像这样继续
from datetime import datetime
import pytz
df['local_time'] = [pytz.timezone('Europe/Berlin').localize(datetime.fromisoformat(t)) for t in df['local_time']]
# so you can make a UTC index:
df.set_index(df['local_time'].dt.tz_convert('UTC'), inplace=True)
# Now you can create a new, hourly index from that and re-index:
dti = pd.date_range(df.index[0], df.index[-1], freq='H')
df2 = df.reindex(dti)
# for comparison, the "re-created" local_time column:
df2['local_time'] = df2.index.tz_convert('Europe/Berlin').strftime('%Y-%m-%d %H:%M:%S').values
那应该给你类似的东西
df2
value local_time
2017-03-25 23:00:00+00:00 2016.0 2017-03-26 00:00:00
2017-03-26 00:00:00+00:00 2017.0 2017-03-26 01:00:00
2017-03-26 01:00:00+00:00 2018.0 2017-03-26 03:00:00
2017-03-26 02:00:00+00:00 2019.0 2017-03-26 04:00:00
2017-03-26 03:00:00+00:00 NaN 2017-03-26 05:00:00
... ...
2017-10-29 00:00:00+00:00 NaN 2017-10-29 02:00:00
2017-10-29 01:00:00+00:00 7224.0 2017-10-29 02:00:00 # note: value randomly attributed to "second" 2 am
2017-10-29 02:00:00+00:00 NaN 2017-10-29 03:00:00
2017-10-29 03:00:00+00:00 7226.0 2017-10-29 04:00:00
2017-10-29 04:00:00+00:00 7227.0 2017-10-29 05:00:00
如上所述,值 7224
归因于 2017-10-29 01:00:00 UTC
,但它也可以归因于 2017-10-29 00:00:00 UTC
如果你不在乎,那很好。如果这是一个问题,我认为你能做的最好的事情就是放弃这个值(value)。您可以使用
df['local_time'] = pd.to_datetime(df['local_time']).dt.tz_localize('Europe/Berlin', ambiguous='NaT')
而不是上面代码中的原生 Python 部分。
关于python - 从 CET/CEST 到 UTC 的时间序列转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70235068/
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 3 年前。 Improve th
当我使用 UTC() 函数获取纪元时间(1970 年 1 月 1 日)的总秒数时。但再次使用“new Date(milliseconds)”构造函数将相同的秒数转换为 Date 并没有给出 UTC d
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 11 年前。 Improve thi
我有一个关于 Swift 类型 TimeZone 的问题。我需要一个 UTC0 时区来格式化我的 Date 对象。我正在像这样创建 TimeZone 对象。 let utcTimeZone = Tim
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 5 年前。 Improve t
我遇到了以下代码: datetime.datetime.utcnow().replace(tzinfo=tzutc()) 我看不到 Replace() 调用正在做什么,从阅读文档来看,它似乎将其转换为
我在应用程序中将时区设置为 UTC,如下所示: // set default time zone to UTC TimeZone.setDefault(TimeZone.getTimeZone(Zon
我需要两件事: 将当前时间转换为 UTC(这样我就可以以 UTC 格式存储日期)--> result = java.util.Date. 将加载日期(UTC 格式)转换为任何时区 --> result
我想将日期从本地转换为 UTC,再将 UTC 转换为本地。 例如。 2015 年 4 月 1 日,12:00 PM 然后是 UTC 2015 年 4 月 1 日下午 5:30 对我来说是本地时间,但我
嗨,我有一个长度为几百万的字符向量( rr ),它以 %Y-%m-%d %H:%M:%S 格式表示时间和日期戳。记录在澳大利亚/悉尼。 如何获得代表这一点的 POSIXct 对象(快速)。 我找到了
static String createUTCTime() { Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC")
我正在尝试将语言环境时间转换为 UTC,然后将 UTC 转换为语言环境时间。但我没有得到结果。 public class DateDemo { public static void main(Stri
我正在寻找用纯 Javascript 替换 moment js 功能 - 我需要重新工作的项目将来不会有 moment.js 可用。我已经有一段时间没有使用 javascript Date 了,所以需
我想获取与 UTC 相关的用户时区,然后将其显示为 UTC +/- 。例如,加利福尼亚用户应显示 UTC -8(或 -7,视情况而定),而巴林用户应显示 UTC +3,等等。 下面的代码并没有告诉我它
我正在尝试将毫秒转换为 UTC 日期对象,如下所示 - var tempDate = new Date(1465171200000); // --> tempDate = Mon Jun 06 201
我一直很困惑为什么下面的代码会导致我的日期从 25 日更改为 24 日 SimpleDateFormat sd = new SimpleDateFormat("dd/MM/yyyy"); DateTi
我需要将字符串转换为 UTC 日期,然后将 UTC 日期转换为本地日期。 这是我的代码: var dateStr = "9/8/2015 12:44:00 PM"; console.log(strto
我正在用 PHP 编写一个 Twitter 网络服务。当用户登录时,我收到此节点: -18000 我必须更改脚本的时区,以便它适应用户的实际时区。我为此找到的唯一 php 函数是: date_defa
在 PHP 文档中,list of supported time zones , UTC 被列出两次: UTC 等/UTC 这两者之间有什么概念上的区别,还是它们只是同义词? 最佳答案 首先回答问题:
在 PHP 文档中,list of supported time zones , UTC 被列出两次: UTC 等/UTC 这两者之间有什么概念上的区别,还是它们只是同义词? 最佳答案 首先回答问题:
我是一名优秀的程序员,十分优秀!