How to change the datetime format in Pandas
You can use dt.strftime
if you need to convert datetime
to other formats (but note that then dtype
of column will be object
(string
)):
import pandas as pd
df = pd.DataFrame({'DOB': {0: '26/1/2016', 1: '26/1/2016'}})
print (df)
DOB
0 26/1/2016
1 26/1/2016
df['DOB'] = pd.to_datetime(df.DOB)
print (df)
DOB
0 2016-01-26
1 2016-01-26
df['DOB1'] = df['DOB'].dt.strftime('%m/%d/%Y')
print (df)
DOB DOB1
0 2016-01-26 01/26/2016
1 2016-01-26 01/26/2016
Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd
Use '%Y-%m-%d'
Ex:
import pandas as pd
df = pd.DataFrame({"Date": ["26-12-2007", "27-12-2007", "28-12-2007"]})
df["Date"] = pd.to_datetime(df["Date"]).dt.strftime('%Y-%m-%d')
print(df)
Output:
Date
0 2007-12-26
1 2007-12-27
2 2007-12-28
Change datetime format to hours only in Pandas
If you only want to take the hours from the date strings, you can use .dt.strftime()
after the pd.to_datetime()
call, as follows:
ds['waktu'] = pd.to_datetime(ds['tanggal'], format='%a %b %d %H:%M:%S %z %Y').dt.strftime('%H:%M:%S')
Note that your format string for pd.to_datetime()
is not correct and need to replace %A
by %a
.
+0000
is the time zone, which you can parse with %z
in the format string.
Demo
ds = pd.DataFrame({'tanggal': ['Fri Apr 23 12:38:07 +0000 2021', 'Thu Apr 22 11:28:17 +0000 2021']})
ds['waktu'] = pd.to_datetime(ds['tanggal'], format='%a %b %d %H:%M:%S %z %Y').dt.strftime('%H:%M:%S')
print(ds)
tanggal waktu
0 Fri Apr 23 12:38:07 +0000 2021 12:38:07
1 Thu Apr 22 11:28:17 +0000 2021 11:28:17
Pandas - Datetime format change to '%m/%d/%Y'
The reason you have to use errors="ignore"
is because not all the dates you are parsing are in the correct format. If you use errors="coerce"
like @phi has mentioned then any dates that cannot be converted will be set to NaT
. The columns datatype will still be converted to datatime64 and you can then format as you like and deal with the NaT
as you want.
Example
A dataframe with one item in Date
not written as Year/Month/Day (25th Month is wrong):
>>> df = pd.DataFrame({'ID': [91060, 91061, 91062, 91063], 'Date': ['2017/11/10', '2022/05/01', '2022/04/01', '2055/25/25']})
>>> df
ID Date
0 91060 2017/11/10
1 91061 2022/05/01
2 91062 2022/04/01
3 91063 2055/25/25
>>> df.dtypes
ID int64
Date object
dtype: object
Using errors="ignore"
:
>>> df['Date'] = pd.to_datetime(df['Date'], errors='ignore')
>>> df
ID Date
0 91060 2017/11/10
1 91061 2022/05/01
2 91062 2022/04/01
3 91063 2055/25/25
>>> df.dtypes
ID int64
Date object
dtype: object
Column Date
is still an object because not all the values have been converted. Running df['Date'] = df['Date'].dt.strftime("%m/%d/%Y")
will result in the AttributeError
Using errors="coerce"
:
>>> df['Date'] = pd.to_datetime(df['Date'], errors='coerce')
>>> df
ID Date
0 91060 2017-11-10
1 91061 2022-05-01
2 91062 2022-04-01
3 91063 NaT
>>> df.dtypes
ID int64
Date datetime64[ns]
dtype: object
Invalid dates are set to NaT and the column is now of type datatime64 and you can now format it:
>>> df['Date'] = df['Date'].dt.strftime("%m/%d/%Y")
>>> df
ID Date
0 91060 11/10/2017
1 91061 05/01/2022
2 91062 04/01/2022
3 91063 NaN
Note: When formatting datatime64, it is converted back to type object so NaT's are changed to NaN. The issue you are having is a case of some dirty data not in the correct format.
How to change datetime format while converting df to to_json using pandas
There is an Open PR for this issue which is still Open No way with to_json to write only date out of datetime.
May be one thing which you could try:
sql_data.to_json(orient='records', date_format='iso', date_unit='s')
Change datetime format in python dataframe
The key parameter for you to pass when using pd.to_datetime is dayfirst=True. Then, use .dt.strftime('%m/%d/%Y') to change to the desired format. I have also given you an example of how to rename a column and read/write to .csv. Again, I understand that you are on mobile, but next time, I would show more effort.
import pandas as pd
# df=pd.read_csv('filename.csv')
# I have manually created a dataframe below, but the above is how you read in a file.
df=pd.DataFrame({'Local time' : ['11.02.2015 00:00:00.000 GMT+0200',
'12.02.2015 00:00:00.000 GMT+0200',
'15.03.2015 00:00:00.000 GMT+0200']})
#Converting string to datetime and changing to desired format
df['Local time'] = pd.to_datetime(df['Local time'],
dayfirst=True).dt.strftime('%m/%d/%Y')
#Example to rename columns
df.rename(columns={'Local time' : 'Date'}, inplace=True)
df.to_csv('filename.csv', index=False)
df
how to change to datetime format in python
You could apply a custom function on your Date column to parse the date values.
import pandas as pd
import io
from datetime import datetime
temp_data=u"""Date
8/1/2021
8-5-2021
8-6-2021:08:00:00 PM
"""
data = pd.read_csv(io.StringIO(temp_data), sep=";", parse_dates=False)
def to_date(string_date):
formats=["%d/%m/%Y","%d-%m-%Y","%d-%m-%Y:%I:%M:%S %p"]
parsed_date=None
for format in formats:
try:
parsed_date=datetime.strptime('8-6-2021:08:00:00 PM', "%d-%m-%Y:%I:%M:%S %p").date()
return parsed_date
except ValueError:
pass
raise RuntimeError(f"Unable to parse date {string_date} with available formats={formats}")
You can add new formats in the to_date function to parse any new date format.
data['Date']=data['Date'].apply(lambda row: to_date(row))
>>> data
Date
0 2021-06-08
1 2021-06-08
2 2021-06-08
How to change datetime format with Pandas
You can use pd.to_datetime
function:
In [1028]: df['date'] = df['date'].apply(pd.to_datetime)
Related Topics
How to Melt a Pandas Dataframe
Tkinter - Executing Functions Over Time
How to Sort a List of Objects Based on an Attribute of the Objects
Wait Until Page Is Loaded With Selenium Webdriver For Python
How to Search and Replace Text in a File
All Combinations of a List of Lists
Flask View Return Error "View Function Did Not Return a Response"
Flask View Raises Typeerror: 'Bool' Object Is Not Callable
C Function Called from Python Via Ctypes Returns Incorrect Value
How to Measure Elapsed Time in Python
How to Save/Restore a Model After Training
Why Is _Init_() Always Called After _New_()
How to Fix "Attempted Relative Import in Non-Package" Even With _Init_.Py
How to Split the Definition of a Long String Over Multiple Lines
How to Get Line Count of a Large File Cheaply in Python
Convert All Strings in a List to Int