manipulating_dates_in_python
Table of Contents
Tasks
- uses | strptime
- convert yyyy-mm-dd to datetimeindex (github.com/KamarajuKusumanchi)
- uses | pd.to_datetime, set_index
today related
get today's date as YYYYMMDD
Approach 1:
from datetime import date date.today().strftime('%Y%m%d')
Approach 2:
from datetime import datetime datetime.today().strftime('%Y%m%d')
I prefer the first approach.
Experiments on the first approach:
% ipython In [1]: from datetime import date date.today().strftime('%Y%m%d') Out[1]: '20240720' In [2]: date.today().strftime('%Y-%m-%d') Out[2]: '2024-07-20' In [3]: date.today().isoformat() Out[3]: '2024-07-20' In [4]: date.today() Out[4]: datetime.date(2024, 7, 20)
Experiments for approach 2:
In [5]: from datetime import datetime datetime.today() Out[5]: datetime.datetime(2024, 7, 20, 21, 30, 45, 642791) In [6]: datetime.today().strftime('%Y%m%d') Out[6]: '20240720' In [7]: datetime.today().strftime('%Y-%m-%d') Out[7]: '2024-07-20' In [8]: datetime.today().isoformat() Out[8]: '2024-07-20T21:31:14.587349'
Dummy
date in isoformat
tags | isodate, iso format
from datetime import date today = date.today().isoformat() print(type(today)) print(today) <class 'str'> 2023-09-18
It also works on datetime objects and will give the time in ISO 8601 format.
from datetime import datetime now = datetime.today().isoformat() print(type(now)) print(now) <class 'str'> 2023-09-18T13:55:17.214513
Ref:- https://stackoverflow.com/questions/32490629/getting-todays-date-in-yyyy-mm-dd-in-python
date and time in isoformat
tags | isotime, iso format
To display time up to milliseconds
In [9]: from datetime import datetime, timezone print(datetime.now(timezone.utc).isoformat(timespec='seconds')) 2024-11-26T22:46:46+00:00 In [10]: from datetime import datetime, timezone print(datetime.now().isoformat(timespec='seconds')) 2024-11-26T17:46:48
So my timezone is GMT-5
extract date from datetime
$ ipython Python 3.10.6 | packaged by conda-forge | (main, Oct 24 2022, 16:02:16) [MSC v.1916 64 bit (AMD64)] Type 'copyright', 'credits' or 'license' for more information IPython 8.4.0 -- An enhanced Interactive Python. Type '?' for help. In [1]: from datetime import datetime s = '24 October 2022' dtt = datetime.strptime(s, "%d %B %Y") print(dtt) 2022-10-24 00:00:00 In [2]: type(dtt) Out[2]: datetime.datetime In [3]: dt = datetime.strptime(s, "%d %B %Y").date() print(dt) 2022-10-24 In [4]: type(dt) Out[4]: datetime.date
get today's day, month and year
% ipython3 Python 3.9.2 (default, Feb 28 2021, 17:03:44) IPython 7.20.0 -- An enhanced Interactive Python. Type '?' for help. In [1]: import datetime today = datetime.date.today() In [2]: print(today) 2021-08-14 In [3]: type(today) Out[3]: datetime.date In [4]: [today.year, today.month, today.day] Out[4]: [2021, 8, 14]
get current time as YYYYMMDD_HHMMSS
from datetime import datetime datetime.now().strftime("%Y%m%d_%H%M%S")
Code snippet:
run_time_stamp = datetime.now().strftime("%Y%m%d_%H%M%S")
iterate over a range of dates
To iterate on all days including weekends
for dt in pd.date_range('20160226', '20160303'): print(dt.strftime('%Y%m%d')) 20160226 20160227 20160228 20160229 20160301 20160302 20160303
To iterate only on weekdays
for dt in pd.date_range('20160226', '20160303'): if (dt.weekday() < 5): print(dt.strftime('%Y%m%d')) 20160226 20160229 20160301 20160302 20160303
To iterate in reverse chronological order
for dt in pd.date_range('20160226', '20160303')[::-1]: if (dt.weekday() < 5): print(dt.strftime('%Y%m%d')) 20160303 20160302 20160301 20160229 20160226
To experiment with just one date element
>>> a = pd.date_range('20160226', '20160303') >>> a[0].strftime('%Y%m%d') '20160226'
To create a pandas.tslib.Timestamp variable
>>> from datetime import datetime >>> for dt in pd.date_range('20160929', '20160930')[::-1]: ... dt == pd.Timestamp(datetime(2016, 9, 29)) ... False True >>> type(pd.Timestamp(datetime(2016, 9, 29))) <class 'pandas.tslib.Timestamp'> >>> for dt in pd.date_range('20160929', '20160930')[::-1]: ... type(dt) ... <class 'pandas.tslib.Timestamp'> <class 'pandas.tslib.Timestamp'>
tags | convert output from pandas data_range function to YYYYMMDD, date_range reverse order
Ref:-
print the names of months in a year
$ ipython Python 3.11.4 | packaged by Anaconda, Inc. | (main, Jul 5 2023, 13:47:18) [MSC v.1916 64 bit (AMD64)] IPython 8.12.0 -- An enhanced Interactive Python. Type '?' for help. In [1]: import calendar for m in calendar.month_name: print(m) January February March April May June July August September October November December
manipulating_dates_in_python.txt · Last modified: 2024/12/04 05:05 by raju