【问题标题】:Python - how to change inbuilt number of days in a monthPython - 如何更改一个月内的内置天数
【发布时间】:2020-11-01 09:27:35
【问题描述】:

我制作了以下情节:

使用这个最少的代码:

import pandas as pd
import matplotlib.pyplot as plt

raw = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63]
test = pd.Series(raw, index=pd.date_range('1-1-2019', periods=len(raw), freq='D'), name = 'MyDebit')
test.plot(grid = True)
plt.show()

有 63 个观察结果,因此 Python 正确地将该系列解读为截至 3 月 4 日:

31 in Jan-19 
28 in Feb-19 
4  in Mar-19
----
63

我目前正在制定一个季节性调整程序,该程序要求所有月份都有 31 天。现在,显然我可以简单地使用整数索引,并在每 31 个观察值处设置主要刻度。

但是,我正在寻找一个“更纯粹”的解决方案,以便我可以显示具有正确日期轴的图并使用日期时间格式。

那么,有没有一种方法可以从根本上改变 Python 日历中的天数,使每个月有 31 天(无论是否闰年;所以每年都会有 372 天)?

【问题讨论】:

  • 对于您想要做的事情,“使用整数索引”听起来很纯粹。修改 Python 内置函数并没有那么多。
  • 只是想一想:在两个月内平均映射这些值不是更好吗,这样您每天的值会略多于一个?这仍然适用于地球。一年 372 天显然是一个不同的星球。
  • @MrFuppes - 理解,但想知道是否有办法重新定义它。
  • @Wups 需要每天一个值;非行星地球日将为空,由三次样条插值填充。

标签: python matplotlib python-datetime


【解决方案1】:

您可以尝试将number = last-day +1 多次附加到蛾的天数数组中= the deference of the day's number and 31

function convertTo31(months):
   if (months.length - 31 = 0):
     return months
   else:
      for (i in range(0,months.length - 31,1)):
        months.append(months[-1]+1)
      return months
        

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多