简体   繁体   English

DataFrame的Python工作日下降

[英]Python weekday drop from DataFrame

I try to drop the weekdays from a dataframe (financial time series) and I keep getting the following error: 我尝试从数据框(财务时间序列)中删除工作日,但不断出现以下错误:

"AttributeError: 'Series' object has no attribute 'weekday'"

Here is my code: 这是我的代码:

df = df[df.date.weekday()  < 5]
df = df.drop(df.date.weekday() < 5)

I tried a few others but nothing seemed to work. 我尝试了其他几个,但似乎没有任何效果。

I looked at dtypes and this is what I get: 我看着dtypes,这就是我得到的:

Unnamed: 0           int64
close              float32
date                object
high               float64
low                float64
open               float64
quoteVolume        float64
volume             float64
weightedAverage    float64
dtype: object

So date is an object, but I can't transform it to datetime, I tried these: 所以date是一个对象,但是我无法将其转换为datetime,我尝试了以下方法:

df['date'] = df.date.astype('date')
df['date'] = df.date.astype('datetime')

both gave me the error: 都给我错误:

TypeError: data type "date" not understood

The time format of the Series is: 2016-09-23 17:00:00 so yyyy-MM-dd hh:mm:ss . 该系列的时间格式为: 2016-09-23 17:00:00 so yyyy-MM-dd hh:mm:ss

Use pd.to_datetime : 使用pd.to_datetime

import pandas as pd
df = df[pd.to_datetime(df.date).dt.weekday < 5]

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

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