Search code examples
pythonpandaspython-datetime

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


I have a dataframe like this:

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
i see below
How to work around Python Pandas DataFrame's "Out of bounds nanosecond timestamp" error?
but yet i cannot do that for my dataframe


Solution

  • 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:

    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