我在 Pandas DataFrame 中有一个列,其中包含带时区的时间戳。本专栏中有两个不同的时区,我需要确保只有一个。这是列末尾的输出:
260003 2019-05-21 12:00:00-06:00
260004 2019-05-21 12:15:00-06:00
Name: timestamp, Length: 260005, dtype: object
Run Code Online (Sandbox Code Playgroud)
对于它的价值,时间戳在-06:00和之间变化-07:00,并具有以下输出:
datetime.datetime(2007, 10, 1, 1, 0, tzinfo=tzoffset(None, -21600))对于-06:00
datetime.datetime(2007, 11, 17, 5, 15, tzinfo=tzoffset(None, -25200))为-07:00
我一直在尝试使用 tz.localize 和 tz.convert,它们过去运行良好,但我认为数据只有一个时区。例如,如果我这样做:
df['timestamp'].dt.tz_localize('MST', ambiguous='infer').dt.tz_convert('MST')
我得到:
ValueError: Array must be all same time zone
During handling of the above exception, another exception occurred:
ValueError: Tz-aware datetime.datetime cannot be converted to datetime64 unless utc=True
Run Code Online (Sandbox Code Playgroud)
有没有办法将这些转换为 MST?或者任何时区,真的吗?我想我可以按时区分解 DataFrame(不是 100% …
我需要将 DataFrame 的长度减少到某个外部定义的整数(可能是两行、10,000 行等,但总长度会减少),但我也想保留生成的 DataFrame 代表原始数据. 原始 DataFrame(我们称之为df)有一个datetime列 ( utc_time) 和一个数据值列 ( data_value)。日期时间始终是连续的、不重复的,但间隔不均匀(即,数据可能“丢失”)。对于此示例中的 DataFrame,时间戳以十分钟为间隔(当数据存在时)。
为了实现这一点,我立即按照以下逻辑进行重采样:找到第一个和最后一个时间戳之间的秒数差,将其除以所需的最终长度,这就是重采样因子。我在这里设置:
# Define the desired final length.
final_length = 2
# Define the first timestamp.
first_timestamp = df['utc_time'].min().timestamp()
# Define the last timestamp.
last_timestamp = df['utc_time'].max().timestamp()
# Define the difference in seconds between the first and last timestamps.
delta_t = last_timestamp - first_timestamp
# Define the resampling factor.
resampling_factor = np.ceil(delta_t / final_length)
# Set the index from …Run Code Online (Sandbox Code Playgroud) 我正在处理一些(杂乱的)遗留代码,我在以下内容中遇到了这个代码段Form_Load:
[other code]
Dim r As Byte
Dim g As Byte
Dim b As Byte
Randomize
[more code]
Run Code Online (Sandbox Code Playgroud)
对于VB的许多部分我还是比较新的,所以请原谅我的无知,如果它是公然的,并且打你的脸,但是有人能告诉我这里发生了什么吗?我对变量声明很好,那些有意义.但是什么是"随机化"只是挂在那里?这应该是VB的随机函数吗?如果是这样,它实际上并没有在这里做任何事情,是吗?我最初认为它是调用原始设计者所写的函数/子函数,但不存在类似的东西.程序中没有错误(虽然有数百个无用的行),如果我评论"随机化",似乎没有任何改变.
dataframe ×2
pandas ×2
python ×2
python-3.x ×2
legacy-code ×1
random ×1
resampling ×1
timezone ×1
vb6 ×1