Duplicate Week and Date Time Values in Python Pandas - python

I am running into an issue trying to convert datetime values consistently into years, weeks, and months.
I was able to figure out how to convert a particular date into a Year/Wk/Month combination, but because of the overlap in week and month numbers, I am encountering duplicate combinations which I want to account for. For example:
2019/ Week 31 / Aug: this is because august 1 is still part of week 31 in the calendar, but the month extracted is in August
2019/ Week 31 / Jul: this is because July 31 is still part of week 31 in the calendar, but the month extracted is in July
My goal is to avoid having duplicates and wrong values extracted. Another example:
2019/ Week 01 / Dec: this is because december 31 is part of week 01 in the new year, and it's tied to calendar year 2019.
This is my code:
req_df is the original dataframe
req_total_grouped for me to group values based on a loc/filter, grouping by datecol which is a date value (ex: 2020-01-01)
import calendar
req_total_grouped = req_df.loc[req_df['datecol'] >= '2019-07-01'].groupby(req_df['datecol'])
req_total_df = req_total_grouped.count()
req_total_df['YEAR'] = req_total_df['datecol'].dt.year
req_total_df['WEEK'] = req_total_df['datecol'].dt.week.map("{:02}".format)
req_total_df['MONTH'] = req_total_df['datecol'].dt.month.apply(lambda x: calendar.month_abbr[x])
req_total_df['YR_WK_MTH'] = req_total_df['YEAR'].astype(str) + \
'/ Week ' + \
req_total_df['WEEK'].astype(str) + \
' / ' \
+ req_total_df['MONTH']
My desired output:
In cases where there are month overlaps, I would want there to be a uniform value. It doesn't matter which month I take they just need to be under the same week. (ex: 2019/ Week 31 / Aug and 2019/ Week 31 / Jul should consolidate into one single value '2019/ Week 31 / Aug' for example)
In cases where there are year over laps (ex: 2019 / Week 01 / Dec) should be 2020 / Week 01 / Jan

I guess grouping the rows by 'year' and 'week' and keeping the last value of each group gives your desired result. Can you try this?
Data (same as yours?)
df = pd.DataFrame({'date': pd.date_range('01/01/2019', '12/31/2020', freq='D')})
df['year'] = df['date'].dt.year
df['month'] = df['date'].dt.month.apply(lambda x: calendar.month_abbr[x])
df['week'] = df['date'].dt.week.map("{:02}".format)
df['yr_wk_mth'] = df['year'].astype(str) + ' / Week ' + df['week'] + ' / ' + df['month']
Code:
print(df.groupby(['year','week'])['yr_wk_mth'].last())
Result:
date month yr_wk_mth
year week
2019 01 2019-12-31 Dec 2019 / Week 01 / Dec
02 2019-01-13 Jan 2019 / Week 02 / Jan
03 2019-01-20 Jan 2019 / Week 03 / Jan
04 2019-01-27 Jan 2019 / Week 04 / Jan
05 2019-02-03 Feb 2019 / Week 05 / Feb
06 2019-02-10 Feb 2019 / Week 06 / Feb
07 2019-02-17 Feb 2019 / Week 07 / Feb
08 2019-02-24 Feb 2019 / Week 08 / Feb
09 2019-03-03 Mar 2019 / Week 09 / Mar
10 2019-03-10 Mar 2019 / Week 10 / Mar
11 2019-03-17 Mar 2019 / Week 11 / Mar
12 2019-03-24 Mar 2019 / Week 12 / Mar
13 2019-03-31 Mar 2019 / Week 13 / Mar
14 2019-04-07 Apr 2019 / Week 14 / Apr
15 2019-04-14 Apr 2019 / Week 15 / Apr
16 2019-04-21 Apr 2019 / Week 16 / Apr
17 2019-04-28 Apr 2019 / Week 17 / Apr
18 2019-05-05 May 2019 / Week 18 / May
19 2019-05-12 May 2019 / Week 19 / May
20 2019-05-19 May 2019 / Week 20 / May
21 2019-05-26 May 2019 / Week 21 / May
22 2019-06-02 Jun 2019 / Week 22 / Jun
23 2019-06-09 Jun 2019 / Week 23 / Jun
24 2019-06-16 Jun 2019 / Week 24 / Jun
25 2019-06-23 Jun 2019 / Week 25 / Jun
26 2019-06-30 Jun 2019 / Week 26 / Jun
27 2019-07-07 Jul 2019 / Week 27 / Jul
28 2019-07-14 Jul 2019 / Week 28 / Jul
29 2019-07-21 Jul 2019 / Week 29 / Jul
30 2019-07-28 Jul 2019 / Week 30 / Jul
31 2019-08-04 Aug 2019 / Week 31 / Aug
32 2019-08-11 Aug 2019 / Week 32 / Aug
33 2019-08-18 Aug 2019 / Week 33 / Aug
34 2019-08-25 Aug 2019 / Week 34 / Aug
35 2019-09-01 Sep 2019 / Week 35 / Sep
36 2019-09-08 Sep 2019 / Week 36 / Sep
37 2019-09-15 Sep 2019 / Week 37 / Sep
38 2019-09-22 Sep 2019 / Week 38 / Sep
39 2019-09-29 Sep 2019 / Week 39 / Sep
40 2019-10-06 Oct 2019 / Week 40 / Oct
41 2019-10-13 Oct 2019 / Week 41 / Oct
42 2019-10-20 Oct 2019 / Week 42 / Oct
43 2019-10-27 Oct 2019 / Week 43 / Oct
44 2019-11-03 Nov 2019 / Week 44 / Nov
45 2019-11-10 Nov 2019 / Week 45 / Nov
46 2019-11-17 Nov 2019 / Week 46 / Nov
47 2019-11-24 Nov 2019 / Week 47 / Nov
48 2019-12-01 Dec 2019 / Week 48 / Dec
49 2019-12-08 Dec 2019 / Week 49 / Dec
50 2019-12-15 Dec 2019 / Week 50 / Dec
51 2019-12-22 Dec 2019 / Week 51 / Dec
52 2019-12-29 Dec 2019 / Week 52 / Dec
2020 01 2020-01-05 Jan 2020 / Week 01 / Jan
02 2020-01-12 Jan 2020 / Week 02 / Jan
03 2020-01-19 Jan 2020 / Week 03 / Jan
04 2020-01-26 Jan 2020 / Week 04 / Jan
05 2020-02-02 Feb 2020 / Week 05 / Feb
06 2020-02-09 Feb 2020 / Week 06 / Feb
07 2020-02-16 Feb 2020 / Week 07 / Feb
08 2020-02-23 Feb 2020 / Week 08 / Feb
09 2020-03-01 Mar 2020 / Week 09 / Mar
10 2020-03-08 Mar 2020 / Week 10 / Mar
11 2020-03-15 Mar 2020 / Week 11 / Mar
12 2020-03-22 Mar 2020 / Week 12 / Mar
13 2020-03-29 Mar 2020 / Week 13 / Mar
14 2020-04-05 Apr 2020 / Week 14 / Apr
15 2020-04-12 Apr 2020 / Week 15 / Apr
16 2020-04-19 Apr 2020 / Week 16 / Apr
17 2020-04-26 Apr 2020 / Week 17 / Apr
18 2020-05-03 May 2020 / Week 18 / May
19 2020-05-10 May 2020 / Week 19 / May
20 2020-05-17 May 2020 / Week 20 / May
21 2020-05-24 May 2020 / Week 21 / May
22 2020-05-31 May 2020 / Week 22 / May
23 2020-06-07 Jun 2020 / Week 23 / Jun
24 2020-06-14 Jun 2020 / Week 24 / Jun
25 2020-06-21 Jun 2020 / Week 25 / Jun
26 2020-06-28 Jun 2020 / Week 26 / Jun
27 2020-07-05 Jul 2020 / Week 27 / Jul
28 2020-07-12 Jul 2020 / Week 28 / Jul
29 2020-07-19 Jul 2020 / Week 29 / Jul
30 2020-07-26 Jul 2020 / Week 30 / Jul
31 2020-08-02 Aug 2020 / Week 31 / Aug
32 2020-08-09 Aug 2020 / Week 32 / Aug
33 2020-08-16 Aug 2020 / Week 33 / Aug
34 2020-08-23 Aug 2020 / Week 34 / Aug
35 2020-08-30 Aug 2020 / Week 35 / Aug
36 2020-09-06 Sep 2020 / Week 36 / Sep
37 2020-09-13 Sep 2020 / Week 37 / Sep
38 2020-09-20 Sep 2020 / Week 38 / Sep
39 2020-09-27 Sep 2020 / Week 39 / Sep
40 2020-10-04 Oct 2020 / Week 40 / Oct
41 2020-10-11 Oct 2020 / Week 41 / Oct
42 2020-10-18 Oct 2020 / Week 42 / Oct
43 2020-10-25 Oct 2020 / Week 43 / Oct
44 2020-11-01 Nov 2020 / Week 44 / Nov
45 2020-11-08 Nov 2020 / Week 45 / Nov
46 2020-11-15 Nov 2020 / Week 46 / Nov
47 2020-11-22 Nov 2020 / Week 47 / Nov
48 2020-11-29 Nov 2020 / Week 48 / Nov
49 2020-12-06 Dec 2020 / Week 49 / Dec
50 2020-12-13 Dec 2020 / Week 50 / Dec
51 2020-12-20 Dec 2020 / Week 51 / Dec
52 2020-12-27 Dec 2020 / Week 52 / Dec
53 2020-12-31 Dec 2020 / Week 53 / Dec

Related

index=False not working due to which not able to use range in skiprow function

I am working on pandas and trying to read a CSV located on my desktop. I actually need the following rows to be published in the defined data frame.
Rows as per CSV = [15,16,17,18,19,21,22,23,24] but as soon as I try to define a range to skip in pandas I start getting NaN Values in my output
import pandas as pd
data = pd.read_csv('C:\\Users\\Michael\\Desktop\\Tradition_Basis_RFR_2021_12_05_15_00_00.csv', header=0, skiprows=1, nrows=22)
df = pd.DataFrame(data, columns=['Name', 'Bid', 'Ask'])
df['Mid_value'] = df['Bid']/2 + df['Ask']/2
print(df)
Script:
Output:
CSV file:
When I only use skiprows=1, in the output I only need red highlighted rows in dataframe:
Try out this code please:
data = pd.read_csv("Tradition_Basis_RFR_2021_12_05_15_00_00.csv", header=0, skiprows=1, nrows=22)
data = data.drop(columns=["0"])
data['Mid_value'] = data['Bid']/2 + data['Ask']/2
which correctly results in data to be like:
Name Date Time Bid Ask Mid_value
0 PLNAB6W2Y 03 DEC 2021 17:54 3.2000 3.2400 3.2200
1 PLNAB6W3Y 03 DEC 2021 17:54 3.1900 3.2300 3.2100
2 PLNAB6W4Y 03 DEC 2021 17:56 3.1700 3.2100 3.1900
3 PLNAB6W5Y 03 DEC 2021 17:54 3.1550 3.1950 3.1750
4 PLNAB6W6Y 03 DEC 2021 17:54 3.1175 3.1575 3.1375
5 PLNAB6W7Y 03 DEC 2021 17:54 3.0800 3.1200 3.1000
6 PLNAB6W8Y 03 DEC 2021 17:54 3.0500 3.0900 3.0700
7 PLNAB6W9Y 03 DEC 2021 17:56 3.0350 3.0750 3.0550
8 PLNAB6W10Y 03 DEC 2021 17:54 3.0400 3.0800 3.0600
9 PLNAB6W12Y 03 DEC 2021 17:54 3.1200 3.1600 3.1400
10 PLNAB6W15Y 03 DEC 2021 17:54 3.2900 3.3300 3.3100
11 PLNAB6W20Y 03 DEC 2021 17:54 3.3900 3.4300 3.4100
12 RUB1YIRS 03 DEC 2021 17:00 9.9200 10.0200 9.9700
13 RUB2YIRS 03 DEC 2021 17:00 9.4900 9.5900 9.5400
14 RUB3YIRS 03 DEC 2021 17:00 9.1900 9.2900 9.2400
15 RUB4YIRS 03 DEC 2021 17:00 8.9900 9.0900 9.0400
16 RUB5YIRS 03 DEC 2021 17:00 8.8500 8.9500 8.9000
17 RUB6YIRS 03 DEC 2021 17:00 8.7700 8.8700 8.8200
18 RUB7YIRS 03 DEC 2021 17:00 8.7200 8.8200 8.7700
19 RUB8YIRS 03 DEC 2021 17:00 8.7000 8.8000 8.7500
20 RUB9YIRS 03 DEC 2021 17:00 8.7000 8.8000 8.7500
21 RUB10YIRS 03 DEC 2021 17:00 8.7100 8.8100 8.7600
Then all you need to do is eitherloc or iloc:
data = data.iloc[12:17, :].append(data.iloc[18:, :])
data = data.drop(columns = ['Date', 'Time'])
which results:
Name Bid Ask Mid_value
12 RUB1YIRS 9.92 10.02 9.97
13 RUB2YIRS 9.49 9.59 9.54
14 RUB3YIRS 9.19 9.29 9.24
15 RUB4YIRS 8.99 9.09 9.04
16 RUB5YIRS 8.85 8.95 8.90
18 RUB7YIRS 8.72 8.82 8.77
19 RUB8YIRS 8.70 8.80 8.75
20 RUB9YIRS 8.70 8.80 8.75
21 RUB10YIRS 8.71 8.81 8.76

Pandas cumulative sum without changing week order number

I have a dataframe which looks like the following:
df:
RY Week no Value
2020 14 3.95321
2020 15 3.56425
2020 16 0.07042
2020 17 6.45417
2020 18 0.00029
2020 19 0.27737
2020 20 4.12644
2020 21 0.32753
2020 22 0.47239
2020 23 0.28756
2020 24 1.83029
2020 25 0.75385
2020 26 2.08981
2020 27 2.05611
2020 28 1.00614
2020 29 0.02105
2020 30 0.58101
2020 31 3.49083
2020 32 8.29013
2020 33 8.99825
2020 34 2.66293
2020 35 0.16448
2020 36 2.26301
2020 37 1.09302
2020 38 1.66566
2020 39 1.47233
2020 40 6.42708
2020 41 2.67947
2020 42 6.79551
2020 43 4.45881
2020 44 1.87972
2020 45 0.76284
2020 46 1.8671
2020 47 2.07159
2020 48 2.87303
2020 49 7.66944
2020 50 1.20421
2020 51 9.04416
2020 52 2.2625
2020 1 1.17026
2020 2 14.22263
2020 3 1.36464
2020 4 2.64862
2020 5 8.69916
2020 6 4.51259
2020 7 2.83411
2020 8 3.64183
2020 9 4.77292
2020 10 1.64729
2020 11 1.6878
2020 12 2.24874
2020 13 0.32712
I created a week no column using date. In my scenario regulatory year starts from 1st April and ends at 31st March of next year that's why Week no starts from 14 and ends at 13. Now I want to create another column that contains the cumulative sum of the value column. I tried to use cumsum() by using the following code:
df['Cummulative Value'] = df.groupby('RY')['Value'].apply(lambda x:x.cumsum())
The problem with the above code is that it starts calculating the cumulative sum from week no 1 not from week no 14 onwards. Is there any way to calculate the cumulative sum without disturbing the week order number?
EDIT: You can sorting values by RY and Week no before GroupBy.cumsum and last sorting index for original order:
#create default index for correct working
df = df.reset_index(drop=True)
df['Cummulative Value'] = df.sort_values(['RY','Week no']).groupby('RY')['Value'].cumsum().sort_index()
print (df)
RY Week no Value Cummulative Value
0 2020 14 3.95321 53.73092
1 2020 15 3.56425 57.29517
2 2020 16 0.07042 57.36559
3 2020 17 6.45417 63.81976
4 2020 18 0.00029 63.82005
5 2020 19 0.27737 64.09742
6 2020 20 4.12644 68.22386
7 2020 21 0.32753 68.55139
8 2020 22 0.47239 69.02378
9 2020 23 0.28756 69.31134
10 2020 24 1.83029 71.14163
11 2020 25 0.75385 71.89548
12 2020 26 2.08981 73.98529
13 2020 27 2.05611 76.04140
14 2020 28 1.00614 77.04754
15 2020 29 0.02105 77.06859
16 2020 30 0.58101 77.64960
17 2020 31 3.49083 81.14043
18 2020 32 8.29013 89.43056
19 2020 33 8.99825 98.42881
20 2020 34 2.66293 101.09174
21 2020 35 0.16448 101.25622
22 2020 36 2.26301 103.51923
23 2020 37 1.09302 104.61225
24 2020 38 1.66566 106.27791
25 2020 39 1.47233 107.75024
26 2020 40 6.42708 114.17732
27 2020 41 2.67947 116.85679
28 2020 42 6.79551 123.65230
29 2020 43 4.45881 128.11111
30 2020 44 1.87972 129.99083
31 2020 45 0.76284 130.75367
32 2020 46 1.86710 132.62077
33 2020 47 2.07159 134.69236
34 2020 48 2.87303 137.56539
35 2020 49 7.66944 145.23483
36 2020 50 1.20421 146.43904
37 2020 51 9.04416 155.48320
38 2020 52 2.26250 157.74570
39 2020 1 1.17026 1.17026
40 2020 2 14.22263 15.39289
41 2020 3 1.36464 16.75753
42 2020 4 2.64862 19.40615
43 2020 5 8.69916 28.10531
44 2020 6 4.51259 32.61790
45 2020 7 2.83411 35.45201
46 2020 8 3.64183 39.09384
47 2020 9 4.77292 43.86676
48 2020 10 1.64729 45.51405
49 2020 11 1.68780 47.20185
50 2020 12 2.24874 49.45059
51 2020 13 0.32712 49.77771
EDIT:
After some discussion solution should be simplify by GroupBy.cumsum:
df['Cummulative Value'] = df.groupby('RY')['Value'].cumsum()

How to split one row into multiple and apply datetime on dataframe column?

I have one dataframe which looks like below:
Date_1 Date_2
0 5 Dec 2017 5 Dec 2017
1 14 Dec 2017 14 Dec 2017
2 15 Dec 2017 15 Dec 2017
3 18 Dec 2017 21 Dec 2017 18 Dec 2017 21 Dec 2017
4 22 Dec 2017 22 Dec 2017
Conditions to be checked:
Want to check if any row contains two dates or not like 3rd row. If present split them into two separate rows.
Apply the datetime on both columns.
I am trying to do the same operation like below:
df['Date_1'] = pd.to_datetime(df['Date_1'], format='%d %b %Y')
But getting below error:
ValueError: unconverted data remains:
Expected Output:
Date_1 Date_2
0 5 Dec 2017 5 Dec 2017
1 14 Dec 2017 14 Dec 2017
2 15 Dec 2017 15 Dec 2017
3 18 Dec 2017 18 Dec 2017
4 21 Dec 2017 21 Dec 2017
5 22 Dec 2017 22 Dec 2017
After using regex with findall get the you date , your problem become a unnesting problem
s=df.apply(lambda x : x.str.findall(r'((?:\d{,2}\s)?(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*(?:-|\.|\s|,)\s?\d{,2}[a-z]*(?:-|,|\s)?\s?\d{,4})'))
unnesting(s,['Date_1','Date_2']).apply(pd.to_datetime)
Out[82]:
Date_1 Date_2
0 2017-12-05 2017-12-05
1 2017-12-14 2017-12-14
2 2017-12-15 2017-12-15
3 2017-12-18 2017-12-18
3 2017-12-21 2017-12-21
4 2017-12-22 2017-12-22

How to generate even calendar dates?

Does anyone know how to generate a list in Calendar in python (or some other platform) with "even days", month and year from 2018 until 2021?
Example:
Sun, 02 Jan 2019
Tue, 04 Jan 2019
Thur, 06 Jan 2019
Sat, 08 Jan 2019
Sun, 10 Jan 2019
Tue, 12 Jan 2019
Thur, 14 Jan 2019
Sat, 16 Jan 2019
Sun, 18 Jan 2019
Tue, 20 Jan 2019
Thur, 22 Jan 2019
and so on, respecting the calendar until 2021.
EDIT:
how to generate in python a calendar list between 2018 and 2022 with 2 formats:
Day of the week, Date Month Year Time (hours: minutes: seconds) - Year-Month-Date Time (hours: minutes: seconds)
Note:
Dates: Peer dates only
schedule: Randomly generated schedules
Example:
Tue, 02 Jan 2018 00:59:23 - 2018-01-02 00:59:23
Thu, 04 Jan 2018 10:24:52 - 2018-01-04 10:24:52
Sat, 06 Jan 2018 04:11:09 - 2018-01-06 04:11:09
Mon, 08 Jan 2018 16:12:40 - 2018-01-08 16:12:40
Wed, 10 Jan 2018 10:08:15 - 2018-01-10 10:08:15
Fri, 12 Jan 2018 07:10:09 - 2018-01-12 07:10:09
Sun, 14 Jan 2018 11:50:10 - 2018-01-14 11:50:10
Tue, 16 Jan 2018 02:29:22 - 2018-01-16 02:29:22
Thu, 18 Jan 2018 19:07:20 - 2018-01-18 19:07:20
Sat, 20 Jan 2018 08:50:13 - 2018-01-20 08:50:13
Mon, 22 Jan 2018 02:40:02 - 2018-01-22 02:40:02
and so on, until the year 2022 ...
Here's something fairly simple that seems to work and handles leap years:
from calendar import isleap
from datetime import date
# Days in each month (1-12).
MDAYS = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
def dim(year, month):
""" Number of days in month of the given year. """
return MDAYS[month] + ((month == 2) and isleap(year))
start_year, end_year = 2018, 2021
for year in range(start_year, end_year+1):
for month in range(1, 12+1):
days = dim(year, month)
for day in range(1, days+1):
if day % 2 == 0:
dt = date(year, month, day)
print(dt.strftime('%a, %d %b %Y'))
Output:
Tue, 02 Jan 2018
Thu, 04 Jan 2018
Sat, 06 Jan 2018
Mon, 08 Jan 2018
Wed, 10 Jan 2018
Fri, 12 Jan 2018
Sun, 14 Jan 2018
Tue, 16 Jan 2018
...
Edit:
Here's a way to do what (I think) you asked how to do in your follow-on question:
from calendar import isleap
from datetime import date, datetime, time
from random import randrange
# Days in each month (1-12).
MDAYS = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
def dim(year, month):
""" Number of days in month of the given year. """
return MDAYS[month] + ((month == 2) and isleap(year))
def whenever():
""" Gets the time value. """
# Currently just returns a randomly selected time of day.
return time(*map(randrange, (24, 60, 60))) # hour:minute:second
start_year, end_year = 2018, 2021
for year in range(start_year, end_year+1):
for month in range(1, 12+1):
days = dim(year, month)
for day in range(1, days+1):
if day % 2 == 0:
dt, when = date(year, month, day), whenever()
dttm = datetime.combine(dt, when)
print(dt.strftime('%a, %d %b %Y'), when, '-', dttm)
Output:
Tue, 02 Jan 2018 00:54:02 - 2018-01-02 00:54:02
Thu, 04 Jan 2018 10:19:51 - 2018-01-04 10:19:51
Sat, 06 Jan 2018 22:48:09 - 2018-01-06 22:48:09
Mon, 08 Jan 2018 06:48:46 - 2018-01-08 06:48:46
Wed, 10 Jan 2018 14:01:54 - 2018-01-10 14:01:54
Fri, 12 Jan 2018 05:42:43 - 2018-01-12 05:42:43
Sun, 14 Jan 2018 21:42:37 - 2018-01-14 21:42:37
Tue, 16 Jan 2018 08:08:39 - 2018-01-16 08:08:39
...
What about:
import datetime
d = datetime.date.today() # Define Start date
while d.year <= 2021: # This will go *through* 2012
if d.day % 2 == 0: # Print if even date
print(d.strftime('%a, %d %b %Y'))
d += datetime.timedelta(days=1) # Jump forward a day
Wed, 31 Oct 2018
Fri, 02 Nov 2018
Sun, 04 Nov 2018
Tue, 06 Nov 2018
Thu, 08 Nov 2018
Sat, 10 Nov 2018
Mon, 12 Nov 2018
Wed, 14 Nov 2018
Fri, 16 Nov 2018
Sun, 18 Nov 2018
Tue, 20 Nov 2018
Thu, 22 Nov 2018
...
Fri, 24 Dec 2021
Sun, 26 Dec 2021
Tue, 28 Dec 2021
Thu, 30 Dec 2021

comparing date using dd/mm/yy format in python

I want to write a program where i can compare current date with couple of dates that i have.
my data is
12 JUN 2016
21 MAR 1989
15 MAR 1958
15 SEP 1958
23 OCT 1930
15 SEP 1928
10 MAR 2010
23 JAN 1928
15 NOV 1925
26 AUG 2009
29 APR 1987
20 JUL 1962
10 MAY 1960
13 FEB 1955
10 MAR 1956
3 MAR 2010
14 NOV 1958
4 AUG 1985
24 AUG 1956
15 FEB 1955
19 MAY 1987
30 APR 1990
8 SEP 2014
18 JAN 2012
14 DEC 1960
1 AUG 1998
7 SEP 1963
9 MAR 2012
1 MAY 1990
14 MAY 1985
15 JUN 1945
5 APR 1995
26 FEB 1987
13 DEC 1983
15 AUG 2009
16 SEP 1980
16 JAN 2005
19 JUN 2011
Now how can i compare this to current date to know that date is not exceeding current date ( i.e 13/JUN/2016).
please help me! Thank you.
You have to create a datetime object using the string data. You can create the object by parsing the date string using strptime method.
from datetime import datetime
mydate = datetime.strptime("19 JUN 2011", "%d %b %Y")
And then use the object to compare it with today's date.
print mydate < datetime.today()
True

Categories