taking date input and grouping pandas datfarme as per time - python-3.x

I have a pandas dataframe with date and time values as follows.
Date Time Pattern
0 06/01/13 0:00:01 A
1 06/02/13 1:00:01 B
2 06/03/13 2:00:01 A
3 06/04/13 3:00:01 C
Now i intend to take date input from user as follows:
date = str(input('Input date in mm-dd-yy format'))
Now how should i find/group by all the rows with input date by user and copy it to a new dataframe. I tried many things but got confused with datatime conversion.
How should i go about it?

First make sure your Date column is datetime
df.Date = pd.to_datetime(df.Date)
Then use query
date = pd.to_datetime(input('Input date in mm-dd-yyyy format'))
df.query('Date == #date')
response to #learningprogramming
You can include other criteria in query
date = pd.to_datetime(input('Input date in mm-dd-yyyy format: '))
df.query('Date == #date & Pattern == "B"')
loc works as well
date = pd.to_datetime(input('Input date in mm-dd-yyyy format: '))
df.loc[(df.Date == date) & (df.Pattern == 'B')]
putting all in the inputs
date = pd.to_datetime(input('Input date in mm-dd-yyyy format: '))
pattern = str(input('Input pattern type: '))
df.query('Date == #date & Pattern == #pattern')

Is the column named 'Date' a string? If so, you can try something like:
subset = df[df['Date'] == date]

Related

Function to get year , month and day from a given age in python

Need a function to get DOB from a given age, so if age = 60 .. output will be in format %m,%d,%Y => '08/16/1962' .. assuming current date from date.today() is 2022-08-16 .. need to change format to be 08-16-2022
Try it like this:
age = int(input)
today = date.today()
todayFormat = today.strftime('%m/%d/%Y')
return todayFormat.replace(year = todayFormat.year - age)
I assumed age is a consoleinput,
date.today creates the current date as date object,
date. replace replaces certain parts of a date.

Pandas to recognize current date, and filter a date column relative to today's date

Having a lot of trouble translating the logic below in pandas/python, so I do not even have sample code or a df to work with :x
I run a daily report, that essentially filters for data from Monday thru the day before what 'Today' is. I have a Date column [ in dt.strftime('%#m/%#d/%Y') format] . It will never be longer than a Monday-Sunday scope.
1) Recognize the day it is 'today' when running the report, and recognize what day the closet Monday prior was. Filter the "Date" Column for the Monday-day before today's date [ in dt.strftime('%#m/%#d/%Y') format ]
2) Once the df is filtered for that, take this group of rows that have dates in the logic above, have it check for dates in a new column "Date2". If any dates are before the Monday Date, in Date2, change all of those earlier dates in 'Date2' to the Monday date it the 'Date' column.
3) If 'Today' is a Monday, then filter the scope from the Prior Monday through - Sunday in the "Date" Column. While this is filtered, do the step above [step 2] but also, for any dates in the "Date2" column that are Saturday and Sunday Dates - changes those to the Friday date.
Does this make sense?
Here're the steps:
from datetime import datetime
today = pd.to_datetime(datetime.now().date())
day_of_week = today.dayofweek
last_monday = today - pd.to_timedelta(day_of_week, unit='d')
# if today is Monday, we need to step back another week
if day_of_week == 0:
last_monday -= pd.to_timedelta(7, unit='d')
# filter for last Monday
last_monday_flags = (df.Date == last_mon)
# filter for Date2 < last Monday
date2_flags = (df.Date2 < last_monday)
# update where both flags are true
flags = last_monday_flags & date2_flags
df.loc[flags, 'Date2'] = last_monday
# if today is Monday
if day_of_week == 0:
last_sunday = last_monday + pd.to_timedelta(6, unit='d')
last_sat = last_sunday - pd.to_timedelta(1, unit='d')
last_week_flags = (df.Date >= last_monday) & (df.Date <= next_sunday)
last_sat_flags = (df.Date2 == last_sat)
last_sun_flags = (df.Date2 == last_sun)
# I'm just too lazy and not sure how Sat and Sun relates to Fri
# but i guess just subtract 1 day or 2 depending on which day
...

Rename substring of column values of a python DataFrame

My problem:
I have a datetime columns, with formats like
'27SEP18:05:02:11'
When trying to convert the datetime values I started like
df['dtimes'] = pd.to_datetime(df['dtimes'],format = '%d%b%Y:%H:%M:%S')
and ran into the problem that 'SEP' is not of the form 'Sep'. Surely I would not like to loop these columns.
Any speed code suggestions, please!?
Use %y for match year in format YY, %Y is used for YYYY format:
#YY format of year - %y
df = pd.DataFrame({'dtimes':['27SEP18:05:02:11','27JAN18:05:02:11']})
df['dtimes'] = pd.to_datetime(df['dtimes'],format = '%d%b%y:%H:%M:%S')
print (df)
dtimes
0 2018-09-27 05:02:11
1 2018-01-27 05:02:11
#YYYY format of year - %Y
df = pd.DataFrame({'dtimes':['27SEP2018:05:02:11','27JAN2018:05:02:11']})
df['dtimes'] = pd.to_datetime(df['dtimes'],format = '%d%b%Y:%H:%M:%S')
print (df)
dtimes
0 2018-09-27 05:02:11
1 2018-01-27 05:02:11

Invalid Dates Python

I am new to Python. I was just wondering, how can you write code that makes beyond a certain date an invalid input. For example, if the user inputs anything after 12/02/2013, it will produce an error. Everything after that date will work perfectly
As glibdud suggested, use datetime objects.
date = datetime.date(YYYY, MM, DD)
where (YYYY, MM, DD) are integers representing years, months, and days. The condition can then be checked in your script with
inputDate > maxDate
for example:
import datetime
maxDate = datetime.date(2013, 12, 2)
y = int(input('Enter year:'))
m = int(input('Enter numerical month (1-12):'))
d = int(input('Enter numerical day (1-31):'))
inputDate = datetime.date(y, m, d)
if inputDate > maxDate:
print('Error - date after 02 December 2013')
else:
print('Success!')
Gives:
Enter year:2018
Enter numerical month (1-12):1
Enter numerical day (1-31):1
Error - date after 02 December 2013
and
Enter year:2000
Enter numerical month (1-12):1
Enter numerical day (1-31):1
Success!

Iterate through CSV and match lines with a specific date

I am parsing a CSV file into a list. Each list item will have a column list[3] which contains a date in the format: mm/dd/yyyy
I need to iterate through the file and extract only the rows which contain a specific date range.
for example, I want to extract all rows for the month of 12/2015. I am having trouble determining how to match the date. Any nudging in the right direction would be helpful.
Thanks.
Method1:
splits your column to month, day and year, converts month and year to integers and then compare and match 12/2015
column3 = "12/31/2015"
month, day, year = column3.split("/")
if int(month) == 12 and int(year) == 2015:
# do your thing
Method2:
parses a datetime string to time object and gets the attributes tm_year and tm_mon, compare them with corresponding month and year.
>>> import time
>>> to = time.strptime("12/03/2015", "%m/%d/%Y")
>>> to.tm_mon
12
>>> to.tm_year
2015

Resources