Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 日期范围内的数据框选择返回,不带行_Python_Pandas_Dataframe - Fatal编程技术网

Python 日期范围内的数据框选择返回,不带行

Python 日期范围内的数据框选择返回,不带行,python,pandas,dataframe,Python,Pandas,Dataframe,我正在遵循通常推荐的流程,以便将数据框合并为我计划用于分析的样本选择,但在使用pd.date\u range和日期之间的选择的过程中,数据丢失,列标题是唯一存在的数据 以下是我的变量: custom_date_start = '2018-01-01' custom_date_end= '2018-10-31' sheet_date = 'date' df_clean # raw data table 简要说明的表格: display(df_clean.head(3)) display(d

我正在遵循通常推荐的流程,以便将数据框合并为我计划用于分析的样本选择,但在使用
pd.date\u range
和日期之间的选择的过程中,数据丢失,列标题是唯一存在的数据

以下是我的变量:

custom_date_start = '2018-01-01'

custom_date_end= '2018-10-31'

sheet_date = 'date'

df_clean # raw data table
简要说明的表格:

display(df_clean.head(3))
display(df_clean.tail(3))

# output

date    b_clicks    b_leads b_sals
2   1/1/2018    72  6   5
3   1/2/2018    232 9   7
4   1/3/2018    255 23  17
date    b_clicks    b_leads b_sals
729 12/29/2019          
730 12/30/2019          
731 12/31/2019      
样本选择:

date_range = pd.date_range(custom_date_start, custom_date_end)

print(date_range)

display(df_clean.head(1))
display(df_clean.tail(1))

df_clean_test = df_clean[(df_clean[sheet_date] > custom_date_start) & (df_clean[sheet_date] <= custom_date_end)]

display(df_clean_test.head(1))
display(df_clean_test.tail(1))

# output

DatetimeIndex(['2018-01-01', '2018-01-02', '2018-01-03', '2018-01-04',
               '2018-01-05', '2018-01-06', '2018-01-07', '2018-01-08',
               '2018-01-09', '2018-01-10',
               ...
               '2018-10-22', '2018-10-23', '2018-10-24', '2018-10-25',
               '2018-10-26', '2018-10-27', '2018-10-28', '2018-10-29',
               '2018-10-30', '2018-10-31'],
              dtype='datetime64[ns]', length=304, freq='D')
date    b_clicks    b_leads b_sals # df_clean
2   1/1/2018    72  6   5
date    b_clicks    b_leads b_sals # df_clean
731 12/31/2019          
date    b_clicks    b_leads b_sals # df_clean_test
date    b_clicks    b_leads b_sals # df_clean_test
date\u range=pd.date\u range(自定义日期开始,自定义日期结束)
打印(日期范围)
显示器(df_清洁头(1))
显示(df_清洁尾翼(1))

df_clean_test=df_clean[(df_clean[工作表日期]>自定义日期开始)和(df_clean[工作表日期]您的逻辑是正确的,但问题在比较中

以这个例子来说,

df1 = pd.DataFrame({'Date': {0: '26/1/2016 ', 1: '27/1/2016 '}})
df1
输出:

      Date
0   26/1/2016
1   27/1/2016
    Date
0   2016-01-26
1   2016-01-27
0    False
1    False
Name: Date, dtype: bool
       Date
0   2016-01-26
1   2016-01-27
0    True
1    True
Name: Date, dtype: bool
其他数据帧

date_range = pd.date_range('2016-01-26', '2016-01-27')
df2 = pd.DataFrame({'Date': date_range})
df2
输出:

      Date
0   26/1/2016
1   27/1/2016
    Date
0   2016-01-26
1   2016-01-27
0    False
1    False
Name: Date, dtype: bool
       Date
0   2016-01-26
1   2016-01-27
0    True
1    True
Name: Date, dtype: bool
让我们比较一下不同格式的日期

print(df2['Date'] == df1['Date'])
输出:

      Date
0   26/1/2016
1   27/1/2016
    Date
0   2016-01-26
1   2016-01-27
0    False
1    False
Name: Date, dtype: bool
       Date
0   2016-01-26
1   2016-01-27
0    True
1    True
Name: Date, dtype: bool
现在,更正df1的
Date
格式

df1['Date'] = pd.to_datetime(df1['Date'])
df1
输出:

      Date
0   26/1/2016
1   27/1/2016
    Date
0   2016-01-26
1   2016-01-27
0    False
1    False
Name: Date, dtype: bool
       Date
0   2016-01-26
1   2016-01-27
0    True
1    True
Name: Date, dtype: bool
让我们再次比较两个数据帧的日期

print(df1['Date'] == df2['Date'])
输出:

      Date
0   26/1/2016
1   27/1/2016
    Date
0   2016-01-26
1   2016-01-27
0    False
1    False
Name: Date, dtype: bool
       Date
0   2016-01-26
1   2016-01-27
0    True
1    True
Name: Date, dtype: bool
在您的示例中,
df_clean
中的
date
格式不正确,因此相比之下,所有值都为false,并且没有返回任何行