I have a column in my dataframe called 'date' as below:
Date Jan-1981 Feb-1981 Mar-1981 . . Sep-2005 . Dec-2008
Is there a way to convert this into datetime format so the default format is DD/MM/YYYY where DD is always 01. eg.
Expected 'Date'
01-01-1981 01-02-1982 . . . . . 01-12-2008
score:8
If need datetimes use to_datetime
:
df['Date'] = pd.to_datetime(df['Date'])
print (df)
0 1981-01-01
1 1981-02-01
2 1981-03-01
3 2005-09-01
4 2008-12-01
If need custom format use Series.dt.strftime
, but datetimes format is lost and get strings (objects):
df['Date'] = pd.to_datetime(df['Date']).dt.strftime('%d-%m-%Y')
print (df)
Date
0 01-01-1981
1 01-02-1981
2 01-03-1981
3 01-09-2005
4 01-12-2008
score:3
If for some reason pd.to_datetime
doesnt parse dates directly (happened to me once where the date format of data was YYYYMMDD with no separators at all) you can get away by using datetime.strptime first, in your case :
import datetime as dt
df['Date'] = pd.to_datetime(df['Date'].apply(lambda x: dt.strptime(x, '%b-%Y')))
Note : the reason you still need to use pd.to_datetime
is because the datetime's and pandas' date-data type are different :
datetime.strptime returns a datetime
object cf the documentation
pandas.to_datetime
returns a Timestamp
or datetime64
cf pandas' documentation
Credit To: stackoverflow.com
Related Query
- convert year and month name into datetime column for pandas dataframe
- How to convert string dataframe column to datetime as format with year and week?
- Convert pandas datetime month to string representation
- Convert datetime string to new columns of Day, Month, Year in pandas data frame
- Convert string Month-Year to datetime in pandas dataframe
- pandas dataframe datetime - convert string to datetime offset
- convert pandas column that is a string from 15 year 4 month 3 day to 15.3 years
- Convert DataFrame column type from string to datetime
- Extracting just Month and Year separately from Pandas Datetime column
- pandas dataframe groupby datetime month
- How to convert column with dtype as object to string in Pandas Dataframe
- Convert Pandas dataframe to csv string
- pandas dataframe convert column type to string or categorical
- Convert Pandas Series to DateTime in a DataFrame
- In Pandas how do I convert a string of date strings to datetime objects and put them in a DataFrame?
- Convert multiple columns to string in pandas dataframe
- Convert the string 2.90K to 2900 or 5.2M to 5200000 in pandas dataframe
- Convert Pandas DataFrame Column From String to Int Based on Conditional
- convert pandas dataframe column from hex string to int
- How do I convert dates into ISO-8601 DateTime format in a Pandas dataframe
- Pandas convert column with year integer to datetime
- Convert array of string (category) to array of int from a pandas dataframe
- Convert a tab- and newline-delimited string to pandas dataframe
- convert a column in a python pandas from STRING MONTH into INT
- Convert a column in pandas dataframe from String to Float
- How do I stop Pandas Dataframe read_json method convert my epoch to human readable string
- Get day of year from a string date in pandas dataframe
- Convert pandas dataframe with Timestamps to String
- Convert string date time to pandas datetime
- Convert epoch time to formatted date string in pandas dataframe
More Query from same tag
- Pandas new column from indexing list by row value
- How to assign to column with non-string "name" or index?
- Set operations on cell level
- Optimizing a loop extracting substrings from a field based on another field
- Pandas Dataframe - sort list element by date, when date is substring of element
- pandas comparision operator `==` not working as expected when column contain `List` instead `Tuple`
- Import CSV using pandas with header size different from content size
- Python pandas dataframe splitting
- How to drop duplicate multiindex column in Pandas
- Groupby and find common string part starting from left in Python
- applying random distribution in each row of data frame
- Changing of Data format from Pivoted data in Dataframes using Pandas Python
- Pandas Strip numbers from a string column in python
- Python merge values from column-row (cell type is list)
- Python Pandas: fillna / bfill using function