每天的范围超出了一个月错误,怀疑其leap年的原因



我的代码似乎不明白有leap年。该代码在非LEAP年数据上效果很好。我遇到的另一个问题是,当我打印出数据时,一年将设置为1900年与实际年份。

def processing(chunk): enter code here
    being read in (by chunksize)
    chunk['Date'] = pd.to_datetime(chunk['Date'], format='%Y-%m-%d') 
    chunk['Year'] = chunk['Date'].dt.year.rename('Year') #creates a new 
        column with the year
    chunk['Month'] = chunk['Date'].dt.month.rename('Month') #new column 
        with month
    chunk['Day'] = chunk['Date'].dt.day.rename('Day') #new column with day
    chunk.drop('Date', 1, inplace=True)
return;
df = pd.read_csv('NLDN_CONUS_flash_and_cloud_2012_dT4KMG.txt', 
    delim_whitespace=True, 
    names=["Date", "Time", "Latitude", "Longitude", "Current", "Multi", 
    "Type"], chunksize=2000000, nrows=2000000)

chunk_list = []
for chunk in df:
    chunk_list.append(chunk)
df_concat = pd.concat(chunk_list)
df_concat['Date'] = pd.to_datetime(df_concat['Date'], format='%Y-%m-%d')
df_concat['month-day'] = df_concat['Date'].dt.strftime('%m-%d')
df_concat['Datetime'] = df_concat['month-day'] + ' ' + df_concat['Time']
df_concat = df_concat[['Datetime', 'Latitude', 'Longitude', 'Current', 
'Multi', 'Type']]
df_concat['Datetime'] = pd.to_datetime(df_concat['Datetime'], format='%m- 
     %d %H:%M:%S.%f')
df_concat.set_index(df_concat['Datetime'], inplace=True)
print(df_concat)

valueerror:一天不在一个月的范围

in

df_concat['Datetime'] = pd.to_datetime(df_concat['Datetime'], format='%m- 
     %d %H:%M:%S.%f')

您将转换为日期时间,而没有任何有关年度的信息。因此,Pandas假定默认年份,即1900年。

我建议将完整的日期时间用作索引,然后按日期或需要进行分组。

最新更新