简体   繁体   English

“ ValueError:日期超出了月份范围”,因为数据中的所有月份中的所有月份共有31天

[英]'ValueError: day is out of range for month' as all the days have 31 days in all month in the data

I am working with 3-hourly satellite precipitation data. 我正在使用3小时一次的卫星降水数据。 However, all the months have got 31 days. 但是,所有月份都有31天。 I have gotten error while appending the datatime. 附加数据时间时出现错误。 Please help! 请帮忙!

sat=pd.read_csv(r"C:\Users\Amod\Documents\Dissertation\Data\MSWEP_INDIA_CITIES_FILTERED\data_11.375_75.875", sep=" ", header=None)


sat.columns = ["year", "month", "day", "satellite"]

years = list(sat.year)
months = list(sat.month)
days = list(sat.day)
rain = list(sat.satellite)

h = 0
datetimes = []
for i in range(len(years)):
    if i ==0:
        h = 0
    else:
        if days[i]==days[i-1]:
            h +=3 #h = h+1 is the same!
        else:
            h = 0
    datetimes.append(datetime.datetime(years[i], months[i], days[i], h))

datetimes

ValueError                                Traceback (most recent call last)
<ipython-input-6-53553b0773c9> in <module>
     10         else:
     11             h = 0
---> 12     datetimes.append(datetime.datetime(years[i], months[i], days[i], h))
     13 
     14 datetimes

ValueError: day is out of range for month

I think you should consider using pandas and filtering out the non-dates but this can be dealt with with a try/except as below: 我认为您应该考虑使用熊猫并过滤掉非日期,但这可以通过try / except处理,如下所示:

sat=pd.read_csv(r"C:\Users\Amod\Documents\Dissertation\Data\MSWEP_INDIA_CITIES_FILTERED\data_11.375_75.875", sep=" ", header=None)



sat.columns = ["year", "month", "day", "satellite"]

years = list(sat.year)
months = list(sat.month)
days = list(sat.day)
rain = list(sat.satellite)

h = 0
datetimes = []
for i in range(len(years)):
    if i ==0:
        h = 0
    else:
        if days[i]==days[i-1]:
            h +=3 #h = h+1 is the same!
        else:
            h = 0
    try:        
        datetimes.append(datetime.datetime(years[i], months[i], days[i], h))
    except: continue

ETA: To deal with errors in the datetime in pandas, check out the "Invalid Data" section here 预计到达时间:要处理熊猫的日期时间错误,请在此处查看“无效数据”部分

df['Date'] = pd.to_datetime(df['Date'], errors = 'coerce')
df = df[df['Date'] != 'NaT']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM