pandas :将日期"object"转换为int [英] Pandas: convert date 'object' to int
问题描述
我有一个Pandas数据框,我需要将带有日期的列转换为int,但是不幸的是,所有给定的解决方案都以错误结尾(如下)
I have a Pandas dataframe and I need to convert a column with dates to int but unfortunately all the given solutions end up with errors (below)
test_df.info()
<class 'pandas.core.frame.DataFrame'>
Data columns (total 4 columns):
Date 1505 non-null object
Avg 1505 non-null float64
TotalVol 1505 non-null float64
Ranked 1505 non-null int32
dtypes: float64(2), int32(1), object(1)
样本数据:
Date Avg TotalVol Ranked
0 2014-03-29 4400.000000 0.011364 1
1 2014-03-30 1495.785714 4.309310 1
2 2014-03-31 1595.666667 0.298571 1
3 2014-04-01 1523.166667 0.270000 1
4 2014-04-02 1511.428571 0.523792 1
我认为我已经尝试了所有方法,但没有任何效果
I think that I've tried everything but nothing works
test_df['Date'].astype(int):
TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是'datetime.date'
TypeError: int() argument must be a string, a bytes-like object or a number, not 'datetime.date'
test_df['Date']=pd.to_numeric(test_df['Date']):
TypeError:位置0处的对象类型无效
TypeError: Invalid object type at position 0
test_df['Date'].astype(str).astype(int):
ValueError:以10为底的int()无效文字:"2014-03-29"
ValueError: invalid literal for int() with base 10: '2014-03-29'
test_df['Date'].apply(pd.to_numeric, errors='coerce'):
将整个列转换为NaNs
Converts the entire column to NaNs
推荐答案
test_df['Date'].astype(int)
给您错误的原因是您的日期仍包含连字符"-".首先通过执行test_df['Date'].str.replace("-","")
抑制它们,然后可以将第一种方法应用于所得的序列.因此,整个解决方案将是:
The reason why test_df['Date'].astype(int)
gives you an error is that your dates still contain hyphens "-". First suppress them by doing test_df['Date'].str.replace("-","")
, then you can apply your first method to the resulting series. So the whole solution would be :
test_df['Date'].str.replace("-","").astype(int)
请注意,如果您的"日期"列不是字符串对象,则此方法将不起作用,通常是在Pandas已将您的系列解析为TimeStamp的情况下.在这种情况下,您可以使用:
test_df['Date'].str.replace("-","").astype(int)
Note that this won't work if your "Date" column is not a string object, typically when Pandas has already parsed your series as TimeStamp. In this case you can use :
test_df['Date'].dt.strftime("%Y%m%d").astype(int)
这篇关于 pandas :将日期"object"转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!