Subtract one date from another and get int [duplicate] - python

This question already has answers here:
How to calculate number of days between two given dates
(15 answers)
Closed 7 years ago.
How can I subtract on datefield from another and get result in integer?
Like 11.05.2015-10.05.2015 will return 1
I tried
entry.start_devation= each.start_on_fact - timedelta(days=data.calendar_start)

Take the difference between two dates, which is a timedelta, and ask for the days attribute of that timedelta.

Related

How do I ask user for date input in python? [duplicate]

This question already has answers here:
Python 3.2 input date function
(5 answers)
Closed 1 year ago.
I want to make def function for schedules.
Here's what I imagine:
User input: yyyy-mm-dd
Output: the next day after the input, next 2 days, next 4 days
I tried to make the function but it doesn't work, please help. Thank you
This question is already answered here:
Python 3.2 input date function
To add 2 days to the input date you would have to do something like:
date = datetime.date(year, month, day)
new_date = date + timedelta(2)

How do I find "n" maximum values for each month in a pandas dataframe? [duplicate]

This question already has answers here:
Pandas get topmost n records within each group
(6 answers)
Closed 3 years ago.
Given a pandas dataframe with company purchases across various months in a year, how do I find the "N" highest each month?
Currently have:
df.groupby(df['Transaction Date'].dt.strftime('%B'))['Amount'].max()
Which is returning the highest value for each month but would like to see the highest four values.
Am I getting close here or is there a more efficient approach? Thanks in advance
With sort_values then tail
yourdf=df.sort_values('Amount').groupby(df['Transaction Date'].dt.strftime('%B'))['Amount'].tail(4)

How can I sort date array [duplicate]

This question already has answers here:
Sort a Python date string list
(2 answers)
Sort list of date strings
(2 answers)
Closed 5 years ago.
Please see my following code snippet
Input
list_x = ["11/1/2100", "5/12/1999", "19/1/2003", "11/9/2001"]
Output
['5/12/1999', '11/9/2001', '19/1/2003', '11/1/2100']
You can convert your day/month format to day in year format and then compare each element in the list based on the year then the day

how to find current day is weekday or weekends in Python? [duplicate]

This question already has answers here:
How do I get the day of week given a date?
(30 answers)
Closed 7 years ago.
Please suggest me on the following.
How to find whether a particular day is weekday or weekend in Python?
You can use the .weekday() method of a datetime.date object
import datetime
weekno = datetime.datetime.today().weekday()
if weekno < 5:
print "Weekday"
else: # 5 Sat, 6 Sun
print "Weekend"
Use the date.weekday() method. Digits 0-6 represent the consecutive days of the week, starting from Monday.

Convert 'float' to 'date' [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I read a date in Excel format in Python?
I am reading values from a .XLS(excel) file.While reading a date field,i am getting a float value like 40374.What i need is to convert this float value into date.Please help.
Thanks in advance!!!!
Dates in excel are represented by the number of days since 1/1/1900. So 40374 is 40,374 days after 1/1/1900, or July 15, 2010
Also I believe that if there is anything after the decimal point, this represents a fraction of a day.
so 40374.0 would be 7/15/2010 at midnight, .5 would be noon, .75 is 6pm, etc.

Categories

Resources