简体   繁体   English

熊猫date_range不返回任何值

[英]Pandas date_range returns no values

I am currently facing a problem regarding the pandas date_range. 我目前遇到有关熊猫date_range的问题。 I have 2 dates in datetime64[ns] type. 我有2个datetime64 [ns]类型的日期。 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. 我试图创建一个数据框,其中包含这两个日期之间的date_range的值作为索引。 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')) (时间戳('2019-07-06 00:00:00'),时间戳('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. 显然您在startend变量中犯了一个错误。 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 : 或者,如果您只想要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 : 也许您可以将index更改为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 . 请注意,列名将采用默认值0

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

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