How to convert object to time - python

How can I convert "2022-03-01 1:01:42 AM" to just 1:01:42?
I tried to strip just the time out and convert to datetime format, but it keeps adding the current date to the beginning. Otherwise, it doesn't properly convert to datetime format so I can plot it later. All I want is the time in datetime format.
def time():
df['Time'] = df['TIME'].apply(lambda x: x.split(' ')[1])
df['Time'] = pd.to_datetime(df.Time, format = '%H:%M:%S', errors='ignore').dt.time

How about using simple date format
from datetime import datetime
now = datetime.now()
print (now.strftime("%H:%M:%S"))

No need to split it. just simply:
import pandas as pd
df = pd.DataFrame({'ID':[1,2],
'TIME':['2022-03-01 1:01:42 AM', '2022-03-01 12:01:42 PM']})
df['Time'] = pd.to_datetime(df.TIME, errors='ignore').dt.time
Output:
df.iloc[0]['Time']
Out[1]: datetime.time(1, 1, 42)

Related

Timestamp to iso

I have a dataframe with timestamp of different formats one with 05-28-2022 14:05:30 and one with 06-04-2022 03:04:13.002 both I want to convert into iso format how can I do that?
input output
05-28-2022 14:05:30 -> 2022-05-28T14:05:30.000+0000
06-04-2022 03:04:13.002 -> 2022-06-04T03:04:13.002+0000
You can use strptime() + strftime(). Here is an example:
from datetime import datetime
import pytz
# parse str to instance
first = datetime.strptime('05-28-2022 14:05:30', '%m-%d-%Y %H:%M:%S')
first = first.replace(tzinfo=pytz.UTC)
print(first.strftime('%Y-%m-%dT%H:%M:%S.%f%z'))
print(f'{first.isoformat()}')
second = datetime.strptime('06-04-2022 03:04:13.002', '%m-%d-%Y %H:%M:%S.%f')
second = second.replace(tzinfo=pytz.UTC)
print(second.strftime('%Y-%m-%dT%H:%M:%S.%f%z'))
print(second.isoformat())
# 2022-05-28T14:05:30.000000+0000
# 2022-05-28T14:05:30+00:00
# 2022-06-04T03:04:13.002000+0000
# 2022-06-04T03:04:13.002000+00:00
See datetime docs. Also you can use other packages for dates processing / formatting:
iso8601
pendulum
dateutil
arrow
Example with dataframe:
import pandas as pd
import pytz
from datetime import datetime
df = pd.DataFrame({'date': ['05-28-2022 14:05:30', '06-04-2022 03:04:13.002']})
def convert_date(x):
dt_format = '%m-%d-%Y %H:%M:%S.%f' if x.rfind('.', 1) > -1 else '%m-%d-%Y %H:%M:%S'
dt = datetime.strptime(x, dt_format).replace(tzinfo=pytz.UTC)
return dt.strftime('%Y-%m-%dT%H:%M:%S.%f%z')
df['new_date'] = df['date'].apply(convert_date)
print(df)
date new_date
0 05-28-2022 14:05:30 2022-05-28T14:05:30.000000+0000
1 06-04-2022 03:04:13.002 2022-06-04T03:04:13.002000+0000

How to get the current date in 'YYYY-MM-DD' and 'datetime64[ns]' format?

I am trying to get the current date in 'YYYY-MM-DD' format, but it seems to be in a String format, and I want it to be in datetime64[ns] format.
So far, I have done this:
>>> import datetime
>>> from datetime import datetime
>>> todays_date = datetime.today().strftime('%Y-%m-%d')
>>> todays_date
'2022-06-06'
I have got the current date, but it is not in the format I need.
How do I convert it to datetime64[ns]?
Thanks.
If you are trying to convert your data to [ns] in pandas this will allow you to achieve the [ns] option
todays_date = datetime.datetime.today().strftime('%Y-%m-%d %X')
df = pd.DataFrame({
'Dates' : [todays_date]
})
df['Dates'] = pd.to_datetime(df['Dates'], infer_datetime_format=True)
df.dtypes
If for whatever reason you want just the date and not the time for your data you can do this to remove the hours/minutes/seconds and it will still be in [ns] format
todays_date = datetime.datetime.today().strftime('%Y-%m-%d 00:00:00')
df = pd.DataFrame({
'Dates' : [todays_date]
})
df['Dates'] = pd.to_datetime(df['Dates'], infer_datetime_format=True)
df.dtypes
pd.to_datetime("today").strftime("%Y/%m/%d")

How To Format Date and Time in Python Using Pandas

I need a way to reformat the date and time from 2021-01-27T12:00:17Z as a separate date and time variable in the format as shown below:
Date: 27/01/2021
Time: 12:00
import pandas as pd
values = {'dates': ['2021-01-27T12:00:17Z']}
df = pd.DataFrame(values)
df['dates'] = pd.to_datetime(df['dates'], format='%Y-%m-%dT%H:%M:%SZ')
formatted_date = pd.to_datetime(df['dates']).dt.date
print('Formatted Date:',formatted_date)
formatted_time = pd.to_datetime(df['dates']).dt.time
print('Formatted Time:',formatted_time)
print ('df value:', df)
print (df.dtypes)
When I change the syntax from format='%Y-%m-%dT%H:%M:%SZ' to format='%d-%m-%YT%H:%M:%SZ' it produces an error.
Any help would be much appreciated.
I am using these, hope it helps;
from datetime import datetime, timedelta, timezone
utc_time = datetime.fromtimestamp(date_time).astimezone(timezone.utc)
local_time = datetime.fromtimestamp(date_time).astimezone(local_tz)
date = datetime.fromisoformat(date_time).astimezone(local_tz).date
time = datetime.fromisoformat(date_time).astimezone(local_tz).time
for datetime calculation, we can use timedelta
local_time = datetime.fromtimestamp(date_time).astimezone(local_tz) + deltatime(hours=5)
local_time = datetime.fromtimestamp(date_time).astimezone(local_tz) + deltatime(minutes=60)
<built-in method date of datetime.datetime object at 0x000002BF40795DA0>
<built-in method time of datetime.datetime object at 0x000002BF40795DA0>
the date and time are datetime.datetime objects.

Subtract 2 datetime lists dd/mm/YYYY in pandas

So, Basically, I got this 2 df columns with data content. The initial content is in the dd/mm/YYYY format, and I want to subtract them. But I can't really subtract string, so I converted it to datetime, but when I do such thing for some reason the format changes to YYYY-dd-mm, so when I try to subtract them, I got a wrong result. For example:
Initial Content:
a: 05/09/2022
b: 30/09/2021
result expected: 25 days.
Converted to DateTime:
a: 2022-05-09
b: 2021-09-30 (For some reason this date stills the same)
result: 144 days.
I'm using pandas and datetime to make this project.
So, I wanted to know a way I can subtract this 2 columns with the proper result.
--- Answer
When I used
pd.to_datetime(date, format="%d/%m/%Y")
It worked. Thank you all for your time. This is my first project in pandas. :)
df = pd.DataFrame({'Date1': ['05/09/2021'], 'Date2': ['30/09/2021']})
df = df.apply(lambda x:pd.to_datetime(x,format=r'%d/%m/%Y')).assign(Delta=lambda x: (x.Date2-x.Date1).dt.days)
print(df)
Date1 Date2 Delta
0 2021-09-05 2021-09-30 25
I just answered a similar query here subtracting dates in python
import datetime
from datetime import date
from datetime import datetime
import pandas as pd
date_format_str = '%Y-%m-%d %H:%M:%S.%f'
date_1 = '2016-09-24 17:42:27.839496'
date_2 = '2017-01-18 10:24:08.629327'
start = datetime.strptime(date_1, date_format_str)
end = datetime.strptime(date_2, date_format_str)
diff = end - start
# Get interval between two timstamps as timedelta object
diff_in_hours = diff.total_seconds() / 3600
print(diff_in_hours)
# get the difference between two dates as timedelta object
diff = end.date() - start.date()
print(diff.days)
Pandas
import datetime
from datetime import date
from datetime import datetime
import pandas as pd
date_1 = '2016-09-24 17:42:27.839496'
date_2 = '2017-01-18 10:24:08.629327'
start = pd.to_datetime(date_1, format='%Y-%m-%d %H:%M:%S.%f')
end = pd.to_datetime(date_2, format='%Y-%m-%d %H:%M:%S.%f')
# get the difference between two datetimes as timedelta object
diff = end - start
print(diff.days)

Pandas DateTime Format not working - python

I am trying to format some dates with datetime, but for some reason it is ignoring my format call. I want day/month/Year format which is what the CSV file has the format is, but when I try this.
df = pd.read_csv('test.csv', parse_dates=['Date'],
date_parser=lambda x: pd.to_datetime(x, format='%d/%m/%Y'))
Result:
Why is it what I can only assume "defaulting" to %Y-%m-%d ???
This should work.
import datetime as dt
import pandas as pd
df = pd.read_csv('test.csv')
formatted_dates =[]
for old_date in df['Date']:
dt_obj = dt.datetime.strptime(old_date,'%d/%m/%Y')
new_date = """{}/{}/{}""".format(dt_obj.day,dt_obj.month,dt_obj.year)
formatted_dates.append(new_date)
df['Date'] = formatted_dates
Output:
18/1/2017
22/1/2017
31/1/2017
...
P.S. There's a bug with the parse_dates,date_parser in pd.read_csv which automatically changes the format to the YYYY-MM-DD.

Categories

Resources