1

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

saeedzali
  • 45
  • 6

1 Answers1

0

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
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • it gives me this error : day is out of range for month because i have day with value of 31 in my dataframe how can i handle this?@jezrael – saeedzali Dec 10 '20 at 11:39
  • @saeedzali - What is your pandas version? – jezrael Dec 10 '20 at 11:40
  • it is '1.0.5' @jezrael – saeedzali Dec 10 '20 at 11:46
  • @saeedzali - problem is with data `housing_deals.head()` ? All with all data? It means working `df['sale_date'].head().apply(lambda x: pd.Period(x, freq='D'))` and not `df['sale_date'].apply(lambda x: pd.Period(x, freq='D'))` ? – jezrael Dec 10 '20 at 11:47
  • @saeedzali - ok, if upgrade to last version I hope help. – jezrael Dec 10 '20 at 11:49
  • i run it on google colab but it gives me same error @jezrael – saeedzali Dec 10 '20 at 12:01
  • i am sorry to take your time. now i want to convert it to datetime to resample it by day but i can not thanks for your answer @jezrael – saeedzali Dec 10 '20 at 14:01
  • @saeedzali - The best post new question if not working `df = df.resample(freq='d', on='sale_date').sum()` – jezrael Dec 10 '20 at 14:03
  • i post it here : https://stackoverflow.com/questions/65185179/how-can-i-resample-pandas-dataframe-by-day-on-period-time @jezrael – saeedzali Dec 10 '20 at 19:40
  • i changed the url. now you can comment there. previous post is closed.@jezrael – saeedzali Dec 11 '20 at 00:18
  • link is here:https://stackoverflow.com/questions/64731501/how-can-i-resample-pandas-dataframe-by-day-on-period-time @jezrael – saeedzali Dec 11 '20 at 00:18