成功解决:ValueError: Of the four parameters: start, end, periods, and freq, exactly three must be specif

成功解决:ValueError: Of the four parameters: start, end, periods, and freq, exactly three must be specified


文章目录

  • 报错问题
  • 报错原因
  • 解决方法


报错问题

我的代码

import pandas as pd
import numpy as np

print(pd.date_range(start='20221001', end='20220601', periods=5, freq='D'))

报错问题

Traceback (most recent call last):
  File "E:/Python/2.py", line 4, in <module>
    print(pd.date_range(start='20221001', end='20220601', periods=5, freq='D'))
  File "D:\Python3.8\lib\site-packages\pandas\core\indexes\datetimes.py", line 1070, in date_range
    dtarr = DatetimeArray._generate_range(
  File "D:\Python3.8\lib\site-packages\pandas\core\arrays\datetimes.py", line 405, in _generate_range
    raise ValueError(
ValueError: Of the four parameters: start, end, periods, and freq, exactly three must be specified

成功解决:ValueError: Of the four parameters: start, end, periods, and freq, exactly three must be specif_第1张图片

报错原因

报错内容翻译ValueError:在四个参数中:start、end、periods和freq,必须指定三个参数

报错原因start,end,periods,freq这4个参数只能设置3个,否则会报错

解决方法

1. 去掉periods参数

import pandas as pd
import numpy as np

print(pd.date_range(start='20221001', end='20220601', freq='D'))

运行结果:

DatetimeIndex([], dtype='datetime64[ns]', freq='D')

2. 去掉freq参数

import pandas as pd
import numpy as np

print(pd.date_range(start='20221001', end='20220601', periods=5))

运行结果:

DatetimeIndex(['2022-10-01 00:00:00', '2022-08-31 12:00:00',
               '2022-08-01 00:00:00', '2022-07-01 12:00:00',
               '2022-06-01 00:00:00'],
              dtype='datetime64[ns]', freq=None)

你可能感兴趣的:(《告别Bug》,python,开发语言)