Python - transform time format in datatime [duplicate] - python

This question already has answers here:
Python: How to convert datetime format? [duplicate]
(2 answers)
Closed 1 year ago.
Have dd-mm-yy in "date" column
05-01-15
need yyyy-mm-dd
2015-01-05
Solved with
df['date'] = pd.to_datetime(df.date, format='%d-%m-%y', errors='coerce')
Has it another solution?

from datetime import datetime
now = datetime.now()
date_time = now.strftime("%Y-%m-%d")
print("date and time:",date_time)
Or have a closer look at strftime documentation.

df['date'] = pd.to_datetime(df['date'])
Will transform your custom format to pandas recognized datetime, which you will be able to use for calculations.

Related

Convert number string to weekday [duplicate]

This question already has answers here:
Converting date/time in YYYYMMDD/HHMMSS format to Python datetime
(3 answers)
How do I get the day of week given a date?
(30 answers)
Closed last month.
How do I convert 20230102 to Monday?
Using python, I need to accomplish this. I have a column of numbers in the format yyyymmdd.
Parse with strptime and format with strftime:
>>> from datetime import datetime
>>> n = 20230102
>>> datetime.strptime(str(n), "%Y%m%d").strftime("%A")
'Monday'
See strftime() and strptime() Format Codes for documentation of the % strings.
You can convert number string into weekday using "datetime" module
import datetime
def get_weekday(date):
date = datetime.datetime.strptime(date, '%Y%m%d')
return date.strftime('%A')
print(get_weekday('20230102'))
This is how you can achieve your desired output.
You can do it with weekday() method.
from datetime import date import calendar
my_date = date.today()
calendar.day_name[my_date.weekday()] #Friday

Changing datetime format [duplicate]

This question already has answers here:
Convert Pandas Column to DateTime
(8 answers)
Closed 3 years ago.
I want to concatenate two dataframes, but they each have two columns that are datetime objects. One is formatted YYYY-MM-DD HH:mm:SS while in the other dataframe it is formateed MM/DD/YEAR HH:mm:SS. Is there way I can convert one format to the other, I am not picky on which one I end up with in the end.
start_time
2018-12-31 23:59:18
and
start_time
4/1/2017 00:13:24
Thanks!
You can convert the format like this
import datetime
date_time_str = '2018-12-31 23:59:18'
date_time_obj = datetime.datetime.strptime(date_time_str, '%Y-%m-%d %H:%M:%S')
date_time_str2 = date_time_obj.strftime('%d/%m/%Y %H:%M:%S')
print(date_time_str2)
Output :
31/12/2018 23:59:18

Convert UTC datetime to local datetime in python [duplicate]

This question already has an answer here:
pandas time shift from utc to local
(1 answer)
Closed 4 years ago.
This is my code :
t = pd.to_datetime(df['timestamp'], unit='ms')
df['Time'] = t.dt.strftime('%H:%M:%S')
df['Hour'] = t.dt.hour
df['ChatDate'] = t.dt.strftime('%d-%m-%Y')
df['ChatDate'] = df['ChatDate']
The 'Time' field in the dataframe is in UTC, how do I get in my local time (Asia/Kolkata) or any other local time?
Use DatetimeIndex.tz_localize and DatetimeIndex.tz_convert:
t.dt.tz_localize('utc').dt.tz_convert('Asia/Calcutta')

Add 1 day to my date in Python [duplicate]

This question already has answers here:
Adding days to a date in Python
(16 answers)
Closed 2 years ago.
I have the following date format:
year/month/day
In my task, I have to add only 1 day to this date. For example:
date = '2004/03/30'
function(date)
>'2004/03/31'
How can I do this?
You need the datetime module from the standard library. Load the date string via strptime(), use timedelta to add a day, then use strftime() to dump the date back to a string:
>>> from datetime import datetime, timedelta
>>> s = '2004/03/30'
>>> date = datetime.strptime(s, "%Y/%m/%d")
>>> modified_date = date + timedelta(days=1)
>>> datetime.strftime(modified_date, "%Y/%m/%d")
'2004/03/31'

Python: Convert string into date format [duplicate]

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 7 years ago.
I know this has been asked a few times, but my scenario is a little different... The objective I need to accomplish is to convert a string of digits '20150425' (which happens to be a date), into a date format such as, '2015-04-25'. I need this because I am trying to compare date objects in my code, but have one variable type represented as a string.
Example below:
date = '20150425' ## want to convert this string to date type format
# conversion here
conv_date = '2015-04-25' ## format i want it converted into
Hope this is clear. Should not be difficult, just do not know how to do it.
This works
from datetime import datetime
date = '20150425'
date_object = datetime.strptime(date, '%Y%m%d')
date_object
>>> datetime.datetime(2015,4,25,0,0)
Assuming the date strings will always be 8 characters:
date = '20150425'
fdate = "{}-{}-{}".format(date[0:4], date[4:6], date[6:]) # 2015-04-25
Alternatively, you can go the "heavier" route and use the actual datetime class:
from datetime import datetime
date = '20150425'
dt = datetime.strptime(date, "%Y%m%d")
dt.strftime("%Y-%m-%d") # 2015-04-25

Categories

Resources