简体   繁体   English

如何在超出日期时间的情况下使用 pandas dataframe?

[英]how can I use pandas dataframe with out of bounds datetime?

I have a dataframe like this:我有一个像这样的 dataframe:

housing_deals.head()
Out[2]: 
         price   sale_date 
0  477,000,000  1396/10/30 
1  608,700,000  1396/11/25 
2  580,000,000  1396/10/03 
3  350,000,000  1396/12/05 
4  328,000,000  1396/03/18 

how can I convert sale_date column to pandas datetime如何将 sale_date 列转换为 pandas 日期时间
i see below我在下面看到
How to work around Python Pandas DataFrame's "Out of bounds nanosecond timestamp" error? 如何解决 Python Pandas DataFrame 的“越界纳秒时间戳”错误?
but yet i cannot do that for my dataframe但我不能为我的 dataframe 做到这一点

You can convert values to daily periods, check docs :您可以将值转换为每日周期,查看文档

df['sale_date'] = df['sale_date'].apply(lambda x: pd.Period(x, freq='D'))
print (df)
         price   sale_date
0  477,000,000  1396-10-30
1  608,700,000  1396-11-25
2  580,000,000  1396-10-03
3  350,000,000  1396-12-05
4  328,000,000  1396-03-18

EDIT: You can convert values to numbers and then use function with docs :编辑:您可以将值转换为数字,然后将 function 与docs一起使用:

print (df['sale_date'].str.replace('/','').astype(int))
0    13961030
1    13961125
2    13961003
3    13961205
4    13960318
Name: sale_date, dtype: int32


def conv(x):
    return pd.Period(year=x // 10000,
                     month=x // 100 % 100,
                     day=x % 100, freq='D')
  

df['sale_date'] = df['sale_date'].str.replace('/','').astype(int).apply(conv)
print (df)

         price   sale_date
0  477,000,000  1396-10-30
1  608,700,000  1396-11-25
2  580,000,000  1396-10-03
3  350,000,000  1396-12-05
4  328,000,000  1396-03-18

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

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