- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用 pandas-0.8rc2 读取包含两列本地化的输入 CSV缺少 UTC 偏移信息的日期时间字符串,需要数据帧系列正确转换为 UTC。
我一直在尝试解决方法来缓解时间戳列都没有的事实代表索引,它们是数据。 tz_localize 和 tz_convert 显然有效仅在系列/数据框的索引上,而不是列。我非常愿意学习更好的方法来做到这一点,而不是以下代码:
# test.py
import pandas
# input.csv:
# starting,ending,measure
# 2012-06-21 00:00,2012-06-23 07:00,77
# 2012-06-23 07:00,2012-06-23 16:30,65
# 2012-06-23 16:30,2012-06-25 08:00,77
# 2012-06-25 08:00,2012-06-26 12:00,0
# 2012-06-26 12:00,2012-06-27 08:00,77
df = pandas.read_csv('input.csv', parse_dates=[0,1])
print df
ser_starting = df.starting
ser_starting.index = ser_starting.values
ser_starting = ser_starting.tz_localize('US/Eastern')
ser_starting = ser_starting.tz_convert('UTC')
ser_ending = df.ending
ser_ending.index = ser_ending.values
ser_ending = ser_ending.tz_localize('US/Eastern')
ser_ending = ser_ending.tz_convert('UTC')
df.starting = ser_starting.index
print df
df.ending = ser_ending.index
print df
其次,代码遇到了一些奇怪的行为。它改变了时间戳第二次赋值回数据框的数据,顺序是否为df.starting 或 df.ending:
$ python test.py
starting ending measure
0 2012-06-21 00:00:00 2012-06-23 07:00:00 77
1 2012-06-23 07:00:00 2012-06-23 16:30:00 65
2 2012-06-23 16:30:00 2012-06-25 08:00:00 77
3 2012-06-25 08:00:00 2012-06-26 12:00:00 0
4 2012-06-26 12:00:00 2012-06-27 08:00:00 77
starting ending measure
0 2012-06-21 04:00:00 2012-06-23 07:00:00 77
1 2012-06-23 11:00:00 2012-06-23 16:30:00 65
2 2012-06-23 20:30:00 2012-06-25 08:00:00 77
3 2012-06-25 12:00:00 2012-06-26 12:00:00 0
4 2012-06-26 16:00:00 2012-06-27 08:00:00 77
Traceback (most recent call last):
File "test.py", line 28, in <module>
print df
File "/path/to/lib/python2.7/site-packages/pandas/core/frame.py", line 572, in __repr__
if self._need_info_repr_():
File "/path/to/lib/python2.7/site-packages/pandas/core/frame.py", line 560, in _need_info_repr_
self.to_string(buf=buf)
File "/path/to/lib/python2.7/site-packages/pandas/core/frame.py", line 1207, in to_string
formatter.to_string(force_unicode=force_unicode)
File "/path/to/lib/python2.7/site-packages/pandas/core/format.py", line 200, in to_string
fmt_values = self._format_col(i)
File "/path/to/lib/python2.7/site-packages/pandas/core/format.py", line 242, in _format_col
space=self.col_space)
File "/path/to/lib/python2.7/site-packages/pandas/core/format.py", line 462, in format_array
return fmt_obj.get_result()
File "/path/to/lib/python2.7/site-packages/pandas/core/format.py", line 589, in get_result
fmt_values = [formatter(x) for x in self.values]
File "/path/to/lib/python2.7/site-packages/pandas/core/format.py", line 597, in _format_datetime64
base = stamp.strftime('%Y-%m-%d %H:%M:%S')
ValueError: year=1768 is before 1900; the datetime strftime() methods require year >= 1900
打印语句只是为了演示问题。不正确的值如果我避免 repr 和其他调用的方法,将无一异常(exception)地执行strftime.
奇怪的是,如果我一直调用 df.{starting,ending} 赋值在 repl 中,我通常会得到一个带有时间戳的正确数据帧:
In [151]: df
Out[151]:
starting ending measure
0 2012-06-21 04:00:00 2012-06-23 11:00:00 77
1 2012-06-23 11:00:00 2012-06-23 20:30:00 65
2 2012-06-23 20:30:00 2012-06-25 12:00:00 77
3 2012-06-25 12:00:00 2012-06-26 16:00:00 0
4 2012-06-26 16:00:00 2012-06-27 12:00:00 77
这是不可重复的,AFAICT,我无法描述调用的确切顺序它超越了上面的 ValueError,但确实如此
如果我遇到错误,我将不胜感激,或者这是不受支持的 API 用法。
如上所述,我宁愿学习更好地使用 pandas API避免这样做。
最佳答案
看来这里可能潜伏着一个错误,所以我在这里创建了一个问题,很快就会看一下并让你知道:
https://github.com/pydata/pandas/issues/1518
编辑:您遇到的错误已修复。我现在也要解决 1900 年前的显示问题。
关于python - pandas read_csv() 输入本地日期时间字符串,tz_convert to UTC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11175213/
我正在使用 MySQL 中的 tz_convert() 函数,但我得到的是 Null。 这正常吗?我读到这是错误? SELECT CONVERT_TZ('2004-01-01 12:00:00','P
当我使用以下代码片段时,我可以看到内容已转换 tweets_df['Time'] = pd.to_datetime(tweets_df['Time']) for index, row in tweet
以下命令似乎适用于亚洲的其他位置,但印度的任何位置除外。这里有错误还是我遗漏了什么?这有效: pd.to_datetime(pd.datetime.now()).tz_localize('US/Pac
我有一些数据,如下所示,UTC 格式为 hour。我想根据 time_zone 创建一个名为 local_hour 的新列。我怎样才能做到这一点?似乎 pandas 的 tz_convert 不允许将
我有一个由 UTC 时间戳索引的数据框,以及指定时区和夏令时与 UTC 的分钟偏移量的 2 列: time_zone daylight_savi
我有一个数据框,其中 hour列包含 UTC 格式的日期时间数据。我有一个 time_zone每个观察的时区列,我用它来转换 hour到本地时间并将其保存在名为 local_hour 的新列中.为此,
我正在尝试根据标准为 Pandas 数据框中的各个行设置不同的时区。作为一名 MWE,以下是我尝试过的方法: test = pd.DataFrame( data = pd.to_datetime(['
我正在使用 pandas-0.8rc2 读取包含两列本地化的输入 CSV缺少 UTC 偏移信息的日期时间字符串,需要数据帧系列正确转换为 UTC。 我一直在尝试解决方法来缓解时间戳列都没有的事实代表索
我的理解是 EST、US/Eastern 和 America/New_York 应该是一样的,但显然我错了。 当我执行以下操作时: pd.Timestamp('2011-07-03T07:00:00-
在下面的脚本中,为什么 tz 和 tz2 不同? import pandas import pytz tz = pytz.timezone('US/Eastern') t = pandas.Times
我是一名优秀的程序员,十分优秀!