Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.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 将.values.tolist()应用于数据帧时,将datetime64列转换为datetime.date_Python_Pandas_Datetime - Fatal编程技术网

Python 将.values.tolist()应用于数据帧时,将datetime64列转换为datetime.date

Python 将.values.tolist()应用于数据帧时,将datetime64列转换为datetime.date,python,pandas,datetime,Python,Pandas,Datetime,我需要将数据帧转换为列表,其中一列的类型为datetime64,但当我在数据帧中应用values.tolist()时,它会转换为Timestamp,我希望它是datetime.date 此数据框从之前读取的Google工作表中获取: def get_dataframe_from_sheet(spreadsheet_id, sheet_name, sheet_range): range_name = '{}!{}'.format(sheet_name, sheet_range) r

我需要将数据帧转换为列表,其中一列的类型为
datetime64
,但当我在数据帧中应用
values.tolist()
时,它会转换为
Timestamp
,我希望它是
datetime.date

此数据框从之前读取的Google工作表中获取:

def get_dataframe_from_sheet(spreadsheet_id, sheet_name, sheet_range):
    range_name = '{}!{}'.format(sheet_name, sheet_range)
    result = service.spreadsheets().values().get(
        spreadsheetId=spreadsheet_id, range=range_name).execute()
    values = result.get('values', [])
    col_names = values.pop(0)

    df = pd.DataFrame(values, columns=col_names)

    for col in col_names:
        if col in ('forecast_month'):
            df[col] = pd.to_datetime(df[col], infer_datetime_format=True)
            df[col] = df.apply()
        else:
            df[col] = pd.to_numeric(df[col], errors='coerce')

    return df

short_term_df = get_dataframe_from_sheet(SHORT_TERM_SPREADSHEET_ID, SHEET_NAME, COLUMNS)
但是,当我应用以下内容时:

LIST__SHORT_TERM_FORECAST_SEASON_ADJ_OCC = short_term_df[base_columns + ['season_adj_occ']].values.tolist()
forecast\u month
列的值属于
Timestamp
类型,我需要
datetime.date
。我怎样才能做到这一点?我已经阅读了几个问题和它们的答案,但它们似乎工作不正常

示例:

这是我从函数中得到的
get\u dataframe\u from\u sheet

    property_id  beds forecast_month  rent_growth  baseline_occ  \
0           329     1     2017-02-01         0.02      0.953623   
1           329     1     2017-03-01         0.02      0.953623   
2           329     1     2017-04-01         0.02      0.953623   
3           329     1     2017-05-01         0.02      0.953623   
4           329     1     2017-06-01         0.02      0.953623   
5           329     1     2017-07-01         0.02      0.953623   
6           329     1     2017-08-01         0.02      0.953623   
7           329     1     2017-09-01         0.02      0.953623   
8           329     1     2017-10-01         0.02      0.953623   
9           329     1     2017-11-01         0.02      0.953623   
10          329     1     2017-12-01         0.02      0.953623   
11          329     1     2018-01-01         0.02      0.953623   
12          329     1     2018-02-01         0.02      0.953623   
13          329     1     2018-03-01         0.02      0.953623   
14          329     1     2018-04-01         0.02      0.953623   
15          329     1     2018-05-01         0.02      0.953623   
16          329     1     2018-06-01         0.02      0.953623   
17          329     1     2018-07-01         0.02      0.953623   
18          329     1     2018-08-01         0.02      0.953623   
19          329     1     2018-09-01         0.02      0.953623   
20          329     1     2018-10-01         0.02      0.953623   
21          329     1     2018-11-01         0.02      0.953623   
22          329     1     2018-12-01         0.02      0.953623 
当我应用
.values.tolist()
时:

试试这个:

df['forecast_month_alt'] = df['forecast_month'].dt.date

显示一个包含数据的小reprexbro@Noobie,我刚刚更新了我的问题,我一分钟前刚试过,是的,它的效果和我预期的一样。非常感谢。
df['forecast_month_alt'] = df['forecast_month'].dt.date