Convert time format in pandas - python

I have a string object in this format 2014-12-08 09:30:00.066000 but I want to convert to datetime variable. I also want this to be less granular- I want it to be just in the order of second for example
2014-12-08 09:30:00.066000 to 2014-12-08 09:30:00
I am trying to use pd.to_datetime function but it's not working for me. Anyone know how to do this? Thanks!

See this:
How to round a Pandas `DatetimeIndex`?
from pandas.lib import Timestamp
def to_the_second(ts):
return Timestamp(long(round(ts.value, -9)))
df['My_Date_Column'].apply(to_the_second)

Related

Converting A timestamp string to the right format

I have a datafield, in the format : '13:55:07 03-01-2023'
This is 3rd of january and not 1st of March
I want to convert this into a timestamp, when I do it directly using
pd.to_datetime(order_data['exch_tm'])
I get the output of the timestamp like this : Timestamp('2023-03-01 13:55:07')
However, this is incorrect as it converting into 1st of march, whereas it should convert it into 3rd of January.
Is the datetime format always the same in the data? If so, what about using a format parameter to pd.to_datetime:
>>> import pandas as pd
>>> pd.to_datetime('13:55:07 03-01-2023', format='%H:%M:%S %d-%m-%Y')
Timestamp('2023-01-03 13:55:07')
You just need to mention the format of date entered.
For eg:
pd.to_datetime(order_data['exch_tm'],format='%H:%M:%S %d-%m-%Y'))
modify it as per your needs and you can find more about datetime is here.

pandas dataframe datetime - convert string to datetime offset

I have a dataframe like:
This time
Time difference
2000-01-01 00:00:00
-3:00
2000-03-01 05:00:00
-5:00
...
...
2000-01-24 16:10:00
-7:00
I'd like to convert the 2nd column (-3:00 means minus 3 hours) from string into something like a time offest that I can directly use to operate with the 1st column (which is already in datetime64[ns]).
I thought there was supposed to be something in pd that does it but couldn't find anything straightforward. Does anyone have any clue?
You can use pd.to_timedelta:
df['Time difference'] = pd.to_timedelta(df['Time difference']+':00')
Obs: I used + ':00' because the default format for string conversion in pd.to_timedelta is "hh:mm:ss".

Formatting datetime in dataframe

I have a dataframe with number of different dates as index:
2005-01-02
2005-01-03
2005-01-04
2005-01-04
...
2014-12-30
2014-12-31
and i want to format them as MM-DD without changing the type to string. Can someone help me with that? And second question: If I do that, can I still use dt.dayofyear?
Simple way
df.index.str[5:]
More common way
df.index.strftime('%m-%d')

How to convert date to datetime?

I have this type of date '20181115 0756' and also in a diffent dataframe in this format '2018-11-15'. I would like to know if there is any way to convert it to datetime without the hours and minutes
date['DATE']= pd.to_datetime(date.DATE)
this converts it to 218-11-15 00:00:00 and I'd like to avoid that
What I trying to do is to calcuate the time difference between the dates in the two dataframes that I have
Thank you in advance
You can use the following code
date['DATE'] = pd.to_datetime(date['DATE'], errors='coerce').dt.date

Find the earliest and oldest date in a list of dates' string representation

How can I convert this to a Python Date so I can find the latest date in a list?
["2018-06-12","2018-06-13", ...] to date
Then:
max(date_objects)
min(date_objects)
Since you want to convert from a list, you'll need to use my linked duplicate with a list comprehension,
from datetime import datetime
list_of_string_dates = ["2018-06-12","2018-06-13","2018-06-14","2018-06-15"]
list_of_dates= [datetime.strptime(date,"%Y-%m-%d") for date in list_of_string_dates]
print(max(list_of_dates)) # oldest
print(min(list_of_dates)) # earliest
2018-06-15 00:00:00
2018-06-12 00:00:00
Basically, you're converting the string representation of your dates to a Python date using datetime.strptime and then applying max and min which are implemented on this type of objects.
import datetime
timestamp = datetime.datetime.strptime("2018-06-12", "%Y-%m-%d")
date_only = timestamp.date()
You can use the datetime module. In particular, since your date is in the standard POSIX format, you'll be able to use the function date.fromtimestamp(timestamp) to return a datetime object from your string; otherwise, you can use the strptime function to read in a more complicated string - that has a few intricacies that you can figure out by looking at the documentation

Categories

Resources