There are dates in this format:

Data 31-01-1982 28-02-1982 31-03-1982 30-04-1982 31-05-1982 

Data type: datetime64[ns]

I need to return the value of only the year: that is, get in this DF at the output:

 Data 1982 1982 1982 1982 1982 

    1 answer 1

    use "accessor" df[col].dt.year :

     In [93]: df Out[93]: Data 0 1982-01-31 1 1982-02-28 2 1982-03-31 3 1982-04-30 4 1982-05-31 In [94]: df['Data'].dt.year Out[94]: 0 1982 1 1982 2 1982 3 1982 4 1982 Name: Data, dtype: int64 

    to replace the values ​​in the column - dates for the year:

     df.loc[:, 'Data'] = df.loc[:, 'Data'].dt.year 
    • And how do I return the DF and not just the values ​​in the column? I wrote that you need to get the same DF, but in a different format - Max52 1:14 pm
    • @ Max52, do you want to change an existing column or add a new one? - MaxU pm
    • In general, just change the existing one - Max52 pm
    • And how to get rid of the error generated? A value is trying to be set on a copy of a slice from a DataFrame . - Max52 pm
    • @ Max52, did you manage to get rid of the error? - MaxU