Home > Mobile >  changing date formate in a data frame python
changing date formate in a data frame python

Time:06-23

I have a data frame with two columns, one are dates and the other are values, the date formate is 1913-01-01T00:00:00.000Z but I want it to change to 1913-01 and remove everything else, the problem is id like to do it for the whole data frame. this is what I have so far

#fixing the format of the date in the DFs and shortning them.
import datetime as dt
inflation['Yearmon'] = pd.to_datetime(inflation['Yearmon'])
inflation.rename(columns = {'Yearmon':'Date'}, inplace = True)

CodePudding user response:

I suggest you to look at the documentation of to_datetime; there's a specific command to send the function to set the format of the function output like:

pd.to_datetime(inflation['Yearmon'], format='%Y%m' , errors='ignore')

hope it helps in some ways, but I'm on holidays and cannot see if it works properly. (don't judge me for being on stackoverflow on holidays hahaha)

CodePudding user response:

Your question is a bit unclear: How does your input look like, exactly. A sample would be helpful (even better would be a MRE, look here and here).

I'm assuming your input looks something like

inflation = pd.DataFrame({
    "Yearmon": ["1913-01-01T00:00:00.000Z", "1913-02-01T00:00:00.000Z", "1914-07-01T00:00:00.000Z"],
    "Value": [0.1, 0.2, 0.3]
})

i.e. you have basically strings in the Yearmon column. If that's the case, then you could simply do

inflation["Yearmon"] = inflation["Yearmon"].str[:7]

to get

   Yearmon  Value
0  1913-01    0.1
1  1913-02    0.2
2  1914-07    0.3

If you actually have datetimes in the Yearmon column, i.e. in the example

inflation["Yearmon"] = pd.to_datetime(inflation["Yearmon"])

then this

inflation["Yearmon"] = inflation["Yearmon"].dt.strftime("%Y-%m")

would produce the same output.

But the result is always a column of strings!

  • Related