This question already has answers here:
python getting weekday from an input date
(2 answers)
Closed 9 years ago.
Have a maths question here which I know to solve using only pen and paper. Takes a while with that approach, mind you. Does anybody know to do this using Python? I've done similar questions involving "dates" but none involving "days". Any of you folk able to figure this one out?
The date on 25/11/1998 is a Wednesday. What is the day on 29/08/2030?
Can anyone at least suggest an algorithm?
Cheers
Use the wonderful datetime module:
>>> import datetime
>>> mydate = datetime.datetime.strptime('29/08/2030', '%d/%M/%Y')
>>> print mydate.strftime('%A')
Tuesday
The algorithm/math is quite simple: There are always 7 days a week. Just calculate how many days between the two days, add it to the weekday of the given day then mod the sum by 7.
<!-- language: python -->
> from datetime import datetime
> given_day = datetime(1998,11,25)
> cal_day = datetime(2030,8,29)
> print cal_day.weekday()
3
> print (given_day.weekday() + (cal_day-given_day).days) % 7
3
Related
This question already has answers here:
Pandas: How to create a datetime object from Week and Year?
(4 answers)
Closed 4 months ago.
I have the following data:
week = [202001, 202002, 202003, ..., 202052]
Where the composition of the variable is [year - 4 digits] + [week - 2 digits] (so, the first row means it's the first week of 2020, and so on).
I want to transform this, to a date-time variable [YYYY - MM - DD]. I'm not sure what day could fit in this format :( maybe the first saturday of every week.
week_date = [2020-01-04, 2020-01-11, 2020-01-18, ...]
It seems like a simple sequence, neverthless I have some missings values on the data, so my n < number of weeks of 2020.
The main purpose of this conversion is that I can have a fit model to train in prophet. I also think I need no missing values when incorporating the data into prophet, so maybe the answer could be also adding 0 to my time series?
Any ideas? Thanks
Try:
l = [202001, 202002, 202003, 202052]
out [datetime.datetime.fromisocalendar(int(x[:4]), int(x[4:]), 6).strftime("%Y-%m-%d") for x in map(str,l)]
print(out)
outputs:
['2020-01-04', '2020-01-11', '2020-01-18', '2020-12-26']
Here I used 6 as the week day but chose as you want
This makes a datetime object from the first and last part of each number after mapping them to a string, then outputs a string back with strftime and the right format.
The below python code worked fine until 12-31-9999.
It threw error once I changed the date to 01-01-10000 (see the screen shot attached)
from datetime import datetime
import time
myNewDate = "12-31-9999"
myNewDateTime = datetime.strptime(myNewDate, '%m-%d-%Y').date()
print (myNewDateTime)
Here is the screen shot:
According to the docs, only 4 digit years are supported.
The years are limited by the 1989 C standard.
%Y signifies a 4 digit year, the last 0 is unmatched by the pattern. You can't use python datetime to work with years beyond 9999: https://docs.python.org/3/library/datetime.html#datetime.MAXYEAR
This question already has answers here:
How do I find the time difference between two datetime objects in python?
(21 answers)
Closed 3 years ago.
When I execute ll command I get the timestamps:
-rw-rw-r--+ 1 4167 May 5 17:19 file A
-rw-rw-r--+ 1 2721 May 4 17:08 file B
I want the difference between timestamps of A and B
I tried this:
datetime.fromtimestamp(getmtime(file)).strftime('%h %m %s'))
It gives
May 05 1557032395
May 04 1557084082
Please help me get the time difference
Looks like you need.
import os
import datetime
print(datetime.datetime.fromtimestamp(os.path.getmtime("file A")) - datetime.datetime.fromtimestamp(os.path.getmtime("file A")))
You can subtract 2 datetime objects to get the difference.
This question already has answers here:
is there any pyspark function for add next month like DATE_ADD(date, month(int type))
(2 answers)
Closed 4 years ago.
I have the table which is updated every day. I use this table for analysis. I want to have a static window of 6 months data as input for analysis.
I know I can make a filter like this in SQL to have 6 months data every time I run the code.
date >= dateadd(mm, -6, getdate())
Can somebody suggest how I can carry on the same action in PySpark. I can only think of this:
df.filter(col("date") >= date_add(current_date(), -6)))
Thanks in advance!
date_add will add or subtract a number of days, in this case use add_months instead:
import pyspark.sql.functions as F
df.filter(F.col("date") >= F.add_months(F.current_date(), -6)))
This question already has answers here:
Python: Number of the Week in a Month
(9 answers)
Closed 7 years ago.
I want to get week number like picture below
If I insert 20150502, It should print "week 1".
If I insert 20150504, It should print "week 2".
If I insert 20150522, It should print "week 4".
How to get week number?
Here's a quick attempt, as with all date/time code there are probably a lot of edge cases that can cause strange results.
# dateutil's parser is very good for converting from string to date
from dateutil.parser import parse
date_strs = ['20150502', '20150504', '20150522']
for date_str in date_strs:
d = parse(date_str)
month_start = datetime.datetime(d.year, d.month, 1)
week = d.isocalendar()[1] - month_start.isocalendar()[1] + 1
print(date_str + ':', week)
Output:
20150502: 1
20150504: 2
20150522: 4