I have data that has a "YEAR" column. The second column is "JFK", which is the wind speeds. I need to convert the YEAR column to show arbitrary months and time. But it needs to keep the year.
For instance, I have some data at the year 1972. I want it the column to read:
1972-01-01 00:00:00.000000000
But instead, I am getting:
1970-01-01 00:00:00.000001972
When I type the following:
winds["YEAR"] = pd.to_datetime(winds["YEAR"],errors='ignore')
For some reason, the year is showing up in seconds. Any help will be appreciated.
CodePudding user response:
Use format=%Y
winds["YEAR"] = pd.to_datetime(winds["YEAR"],format='%Y')
Also, this solution assume that your input is an integer/float since it would correctly parse a string without the need for format.
CodePudding user response:
You first need to parse the given column into a datetime and then format the resulting datetime into a string.
Demo:
import pandas as pd
df = pd.DataFrame({'year': [1972]})
df['year'] = pd.to_datetime(df['year'], format='%Y').dt.strftime('%Y-%m-%d 00:00:00.000000000')
print(df)
Output:
year
0 1972-01-01 00:00:00.000000000