Home > Mobile >  Application of datetime and functools is not clear
Application of datetime and functools is not clear

Time:01-27

I struggle to understand below code. Based on the documentation I read

  1. apply invokes function partial on every value in the series df['startdate']
  2. next, partial function passes argument dayfirst=False to date_change function (I am note sure why partial function is used here)
  3. Also, data table (shown below) indicates that data looks the same: before and after (mainly order) -> my first thought was that this function removes timestamp order in the dataset?
    from datetime import timedelta
    from functools import partial
    
    df['startdate'].apply(partial(date_change, dayfirst=False))

Data table before and after (looks the same: shape and order)

0       2019-12-17
1       2019-12-18
2       2019-12-19
3       2019-12-20
4       2019-12-21
5       2021-10-28
6       2021-10-29

CodePudding user response:

partial returns a callable equivalent to date_change where dayfirst=False.

Basically, this is equivalent to calling data_change(value, dayfirst=False) on each value in df['startdate'].

CodePudding user response:

Are you trying to convert your date string to datetime?

df['startdate2'] = pd.to_datetime(df['startdate'], dayfirst=True)

Output:

# It looks like the same
>>> df
    startdate startdate2
0  2019-12-17 2019-12-17
1  2019-12-18 2019-12-18
2  2019-12-19 2019-12-19
3  2019-12-20 2019-12-20
4  2019-12-21 2019-12-21
5  2021-10-28 2021-10-28
6  2021-10-29 2021-10-29

# But in fact, this is not the case
>>> df.dtypes
startdate             object
startdate2    datetime64[ns]
dtype: object
  •  Tags:  
  • Related