滑动 windows - 测量每个循环的观察长度 window
Sliding windows - measuring length of observations on each looped window
让我们分析一下这个示例代码,其中 zip() 用于从数据集创建不同的 windows 并在循环中 return 它们。
months = [Jan, Feb, Mar, Apr, May]
for x, y in zip(months, months[1:]):
print(x, y)
# Output of each window will be:
Jan Feb
Feb Mar
Mar Apr
Apr May
假设现在我想计算每个 window.
中使用的月份之间的相应长度百分比
步骤示例:
- 当 returning 第一个 window(1 月 2 月)时,我想计算整个 window 中 1 月的百分比长度(等于 1 月 + 2 月)和return 它是一个新变量
- 当 returning 第二个 window(Feb Mar)时,我想计算整个 window(等于 Feb + Mar)中 2 月的百分比长度,并且return 它是一个新变量
- 继续这个过程直到最后window
欢迎就我如何在 for 循环中实现这个想法提出任何建议!
谢谢!
编辑
months = [Jan, Feb, Mar, Apr, May]
for x, y in zip(months, months[2:]):
print(x, y)
# Output of each window will be:
Jan Feb March
Feb Mar Apr
Mar Apr May
目标是在整个 window 长度上计算每个 window 两个月的长度:
- 第一个 window:1 月 + 2 月/1 月 + 2 月 + 3 月
- 第 2 window:2 月 + 3 月/2 月 + 3 月 + 4 月
- 持续到最后window
我们现在可以计算每个 window (含 start.month) 的一个月。但是,我们如何调整它以包含超过一个月的时间?
此外,除了使用 days_in_month,有没有办法使用每个月的数据点(行)的长度?
通过使用数据点(行)的长度,我的意思是每个月都有许多 'time' 格式的数据点(例如,60 分钟格式)。这意味着一个月中的 1 天会有 24 个不同的数据点(行)。
示例:
column
rows
01-Jan-2010 T00:00 value
01-Jan-2010 T01:00 value
01-Jan-2010 T02:00 value
... ...
01-Jan-2010 T24:00 value
02-Jan-2010 T00:00 value
... ...
谢谢!
这是一种方法。 (在我的例子中,months
是一个 period_range
对象。)
import pandas as pd
months = pd.period_range(start='2020-01', periods=5, freq='M')
现在,迭代范围。每次迭代都是 two-month window.
# print header labels
print('{:10s} {:10s} {:>10s} {:>10s} {:>10s} {:>10s} '.format(
'start', 'end', 'month', 'front (d)', 'total (d)', 'frac'))
for start, end in zip(months, months[1:]):
front_month = start.month
# number of days in first month (e.g., Jan)
front_month_days = start.days_in_month
# number of days in current sliding window (e.g., Jan + Feb)
days_in_curr_window = (end.end_time - start.start_time).days
frac = front_month_days / days_in_curr_window
print('{:10s} {:10s} {:10d} {:10d} {:10d} {:10.3f}'.format(
str(start), str(end), front_month,
front_month_days, days_in_curr_window, frac))
start end month front (d) total (d) frac
2020-01 2020-02 1 31 60 0.517
2020-02 2020-03 2 29 60 0.483
2020-03 2020-04 3 31 61 0.508
2020-04 2020-05 4 30 61 0.492
让我们分析一下这个示例代码,其中 zip() 用于从数据集创建不同的 windows 并在循环中 return 它们。
months = [Jan, Feb, Mar, Apr, May]
for x, y in zip(months, months[1:]):
print(x, y)
# Output of each window will be:
Jan Feb
Feb Mar
Mar Apr
Apr May
假设现在我想计算每个 window.
中使用的月份之间的相应长度百分比步骤示例:
- 当 returning 第一个 window(1 月 2 月)时,我想计算整个 window 中 1 月的百分比长度(等于 1 月 + 2 月)和return 它是一个新变量
- 当 returning 第二个 window(Feb Mar)时,我想计算整个 window(等于 Feb + Mar)中 2 月的百分比长度,并且return 它是一个新变量
- 继续这个过程直到最后window
欢迎就我如何在 for 循环中实现这个想法提出任何建议!
谢谢!
编辑
months = [Jan, Feb, Mar, Apr, May]
for x, y in zip(months, months[2:]):
print(x, y)
# Output of each window will be:
Jan Feb March
Feb Mar Apr
Mar Apr May
目标是在整个 window 长度上计算每个 window 两个月的长度:
- 第一个 window:1 月 + 2 月/1 月 + 2 月 + 3 月
- 第 2 window:2 月 + 3 月/2 月 + 3 月 + 4 月
- 持续到最后window
我们现在可以计算每个 window (含 start.month) 的一个月。但是,我们如何调整它以包含超过一个月的时间?
此外,除了使用 days_in_month,有没有办法使用每个月的数据点(行)的长度?
通过使用数据点(行)的长度,我的意思是每个月都有许多 'time' 格式的数据点(例如,60 分钟格式)。这意味着一个月中的 1 天会有 24 个不同的数据点(行)。 示例:
column
rows
01-Jan-2010 T00:00 value
01-Jan-2010 T01:00 value
01-Jan-2010 T02:00 value
... ...
01-Jan-2010 T24:00 value
02-Jan-2010 T00:00 value
... ...
谢谢!
这是一种方法。 (在我的例子中,months
是一个 period_range
对象。)
import pandas as pd
months = pd.period_range(start='2020-01', periods=5, freq='M')
现在,迭代范围。每次迭代都是 two-month window.
# print header labels
print('{:10s} {:10s} {:>10s} {:>10s} {:>10s} {:>10s} '.format(
'start', 'end', 'month', 'front (d)', 'total (d)', 'frac'))
for start, end in zip(months, months[1:]):
front_month = start.month
# number of days in first month (e.g., Jan)
front_month_days = start.days_in_month
# number of days in current sliding window (e.g., Jan + Feb)
days_in_curr_window = (end.end_time - start.start_time).days
frac = front_month_days / days_in_curr_window
print('{:10s} {:10s} {:10d} {:10d} {:10d} {:10.3f}'.format(
str(start), str(end), front_month,
front_month_days, days_in_curr_window, frac))
start end month front (d) total (d) frac
2020-01 2020-02 1 31 60 0.517
2020-02 2020-03 2 29 60 0.483
2020-03 2020-04 3 31 61 0.508
2020-04 2020-05 4 30 61 0.492