【问题标题】:Obtaining decimal format for range of years and specific months获取年份范围和特定月份的十进制格式
【发布时间】:2022-01-05 16:28:39
【问题描述】:

我有月度数据(1993 年 - 2019 年),但我希望获得 1993 年 - 2019 年期间只有 7 月、8 月和 9 月的十进制格式。

以下是 1993 年至 2019 年(所有 12 个月)之间的十进制格式月份的代码,但希望得到相同的结果,但仅适用于 7 月、8 月和 9 月:

year_start = 1993
year_end = 2019

full_time_months = np.arange(year_start+.5/12,year_end+1,1/12)

print(full_time_months[:12])

# these are the 12 months in 1993 as decimals

1993.04166667 1993.125      1993.20833333 1993.29166667 1993.375
 1993.45833333 1993.54166667 1993.625      1993.70833333 1993.79166667
 1993.875      1993.95833333

我的目标是获得一系列月份的 7 月、8 月和 9 月:

1993.54167、1993.625、1993.708... 2019.54167、2019.625、2019.708

其中 year.54167 = 七月,year.625 = 八月,year.708 = 九月。

我该怎么做呢?希望我的问题足够清楚,如果有不清楚的地方请评论,谢谢!!!

【问题讨论】:

  • 为什么?使用 DateTime 对象 - 不是浮动 ....
  • 不幸的是,我在绘制和使用日期时间对象时遇到了更改 xticks 的问题,但是使用 numpy 浮点数时 xticks、绘图等没有问题:/

标签: python numpy date datetime jupyter


【解决方案1】:

我不确定你想用这个实现什么,但你可以做这样的事情来分离你想要的数据。

import numpy as np

year_start = 1993
year_end = 2019


full_time_months = np.arange(year_start+.5/12,year_end+1,1/12)
# Reshape into 2D array
full_time_months = full_time_months.reshape(-1, 12)

# Choose selected columns
# July, Aug, Sept
selected_months = full_time_months[:, [6,7,8]]
print(selected_months)

结果:

[[1993.54166667 1993.625      1993.70833333]
 [1994.54166667 1994.625      1994.70833333]
...
 [2018.54166667 2018.625      2018.70833333]
 [2019.54166667 2019.625      2019.70833333]]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多