简体   繁体   中英

Pandas date_range returns no values

I am currently facing a problem regarding the pandas date_range. I have 2 dates in datetime64[ns] type. The start date and the end date. I am trying to create a dataframe containing the values of date_range between those 2 dates, as index. However, the dataframe created is empty, even though it is supposed to contain values.

Note that when I copy pasted that code and used it in the english version of the site it worked fine. I am facing challenges with the greek one.

The code I wrote is:

customdatedf = pd.DataFrame(index = pd.date_range(start, end, freq='D'))

start and end date are defined from another dataframe like this:

start = df['Date'].iloc[0]
end = df['Date'].iloc[-1]

and their values are returned correctly, as it appears in

print(start, end)

(Timestamp('2019-07-06 00:00:00'), Timestamp('2019-06-26 00:00:00'))

This is printed

Expected result is a dataframe having as index the dates between start and end date

Apparently you made an mistake with your start and end variables. Since the start is after the end variable, so to fix this turn them around:

start = pd.Timestamp('2019-07-06 00:00:00') 
end = pd.Timestamp('2019-06-26 00:00:00')

pd.DataFrame({'Col_dummy':['Dummy']}, index=pd.date_range(end, start, freq='D'))

           Col_dummy
2019-06-26     Dummy
2019-06-27     Dummy
2019-06-28     Dummy
2019-06-29     Dummy
2019-06-30     Dummy
2019-07-01     Dummy
2019-07-02     Dummy
2019-07-03     Dummy
2019-07-04     Dummy
2019-07-05     Dummy
2019-07-06     Dummy

Or if you only want an index :

pd.DataFrame(index=pd.date_range(end, start, freq='D'))

Empty DataFrame
Columns: []
Index: [2019-06-26 00:00:00, 2019-06-27 00:00:00, 2019-06-28 00:00:00, 2019-06-29 00:00:00, 2019-06-30 00:00:00, 2019-07-01 00:00:00, 2019-07-02 00:00:00, 2019-07-03 00:00:00, 2019-07-04 00:00:00, 2019-07-05 00:00:00, 2019-07-06 00:00:00]

Maybe you could change index into data :

customdatedf = pd.DataFrame(data = pd.date_range(start, end, freq='D'))

Please take note that the column name will take default value of 0 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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