changing time format in pandas - python

I have a dataframe with a column datetime that looks like this 2020-05-03T14:51:31.23625 (I assume %Y-%m-%dT%H:%M:%S)
I would like to change it to dd/mm/yyyy hh:mm:ss format.
I found this post and I tried something similar (code below) but it works ony for the first row of the dataframe. Could someone help me to find the mistake? Thanks!
df['time']=pd.DataFrame({'time':pd.to_datetime(df['time'])})
df['new'] = df['time'].dt.strftime("%d/%m/%Y %H:%M:%S")
[![enter image description here][2]][2]

Try via split() and to_datetime() method:
df['datetime']=pd.to_datetime(df['datetime'].str.split('.').str[0],errors='coerce')

Related

How to changed date format from an API? Python Pandas

I download some info from an API. it has this format 1657762387 a whole integer.
How can I convert it the right way?
I tried pd.todatetime(df['colum']) and didn't worked I got some erroneous random date.
Thank you
You need use the unit argument
df['colum'] = pd.to_datetime(df['colum'], unit='s')
print(df)
colum
0 2022-07-14 01:33:07

ValueError: time data '2017-02-22' does not match format '%d-%m-%Y

i'm getting ValueError: time data '2017-02-22' does not match format '%d-%m-%Y this error
for the below code.
if code is null date format looks like this '22-02-2017' else it looks like this in format '2017-02-22'
previously i used to have null data in code column, but now i don't have.
so i'm using below code to make changes .
x1['my_date']=np.where(x1['code'].isnull(),pd.to_datetime(x1['my_date'],format="%d-%m-
%Y"),x1['my_date'])
not able to figure out what's wrong with this code
In your solution add errors='coerce' for working if no match (then is returned NaT):
x1['my_date']=np.where(x1['code'].isnull(),
pd.to_datetime(x1['my_date'],format="%d-%m-%Y", errors='coerce'),
x1['my_date'])

Issue in converting Timetstamp format

In my data frame I have a column which contains timestamps. Now these timestamps are in the format(yyyy-mm-dd hh:mm:ss) and I want to change them to (dd--mm-yyyy hh:mm:ss). I have tried to do so but only the first row is changing properly and the rest of the rows are converting to epoch time i think.
Snapshot of Dataframe
what I tried
the other way I tried
As you can see only the first row is changing whereas the other rows are not. Please help guys!!!
I believe is because of the data type of your column in pandas. If you want to follow your previous attempts, you could just create a new column and fill the data as a string like this:
df_sch["UTC Formatted"] = [datetime.datetime.strftime(entity, "%d-%m-%Y %H:%M%S") for entity in df_sch["UTC"]]
In this sense the data will be stored as string! Hope this helps!
You can try with this instruction:
df_sch['UTC'] = df_sch["UTC"].dt.strftime('%d-%m-%Y %H:%M:%S')
This will convert all UTC column values in your dataframe with the wanted format

Formatting datetime pandas

I have some rows in my dataset with the following release date format:
1995-10-30
It is an object/string. However, I want to convert it to datetime, so I wrote the following to achieve that:
movies_df["release_date"] = pd.to_datetime(movies_df.release_date)
It gets converted to datetime as it should, but I would like to have the following format
mm-dd-year
I have tried yearfirst=False and dayfirst=False but nothing seems to be happening and I cant figure out why it isnt working.
I have also tried to specify the format in the to_datetime method as following:
movies_df["release_date"] = pd.to_datetime(movies_df.release_date, format="%Y/%m/%d", dayfirst=False, yearfirst=False)
Any help is appriciated
You can convert datetimes to strings with format mm-dd-YY:
movies_df["release_date"] = pd.to_datetime(movies_df.release_date).dt.strftime('%m-%d-%Y')
But if want datetimes in format mm-dd-YY it is not possible in python.

Can the date format of dataframe and csv file be the same?

The two photos that I've attached below show a dataframe table and a table that was exported out to csv file. I'm wondering if there is any command that can modify the date so that the dates shown on both files would be the same.
On the dataframe: 2017-08-01 -> but after exporting out it becomes 2017/8/1(Instead ->2017/08/01).
Does anyone know how it can be done, or do I can only manually edit the cell format?
[
pandas.DataFrame.to_csv
When you make the call to the to_csv function, you can supply it the parameter date_format='%Y-%m-%d'.
Check out the documentation. One of the parameters that you can pass to_csv is date_format which allows you to control the format of your date like columns. The format is the same as for datetime
df.to_csv(file_path, date_format="%Y-%m-%d")
The form YYYY-MM-DD should be the default output date format for to_csv().
It seems like you are opening the output CSV in a program that may be applying its own style/formatting to the dates. Try opening it in a text editor to confirm.
sometimes, the way others answer(date_format) doesn't work although it is a right way.
you should just change your cell format on Excel in that case.
In that case, follow this way:
right click => Format Cell => Category => Custom => Type: yyyy-mm-dd

Categories