I found similar posts but had issues with them.
The problem I have is:
I have a date in the format year, month, day example - 2019:01:21
and I want to use a loop likely something like
for i in range(365):
# add to the date by a single day and return something in the form 2019:01:22
You could use datetime.strptime() to parse the given date in string format to a python date format. Then you could use datetime.timedelta() to specify an interval of n days and shift the parsed date by this delta. Here is a short demo:
from datetime import datetime, timedelta
date = datetime.strptime("2019:01:21", "%Y:%m:%d")
for i in range(365):
delta = timedelta(days=1 + i)
print(date + delta)
Output:
2019-01-22 00:00:00
2019-01-23 00:00:00
2019-01-24 00:00:00
...
Related
I have a dataset with a column "Day" that has very specific time entries like the following. How can I transform these entries into month?
From
Day
31MAY2019:00:00:00
29MAY2020:00:00:00
30APR2021:00:00:00
To
Day
May 2019
May 2020
Apr 2021
You just need to use the datepart() function to get extract the date from the datetime value.
data want;
format datetime datetime20. date monyy7.;
datetime = '31MAY2019:00:00:00'dt;
date = datepart(datetime);
run;
Keep in mind this doesn't add the space in between the month and year and is still a number format. If you want this as a character you can do the following:
data want;
format datetime datetime20.;
datetime = '31MAY2019:00:00:00'dt;
date = put(datepart(datetime),monname3.)||' '||put(datepart(datetime),year.);
run;
I have a Pandas data frame which contains a column with dates. The dates are represented in by a string in the format mm/dd/yyyy.
But I have a problem with the format of the day: Dates until the 9th day of a month are in the format mm/d/yyyy. For example the first december 2008 is displayed as 12/1/2008. The 10th day until the end of a month are displayed as mm/dd/yyyy. For example the 17th december 2008 is represented by 12/17/2008.
My target is to transform the all dates into the form mm.dd.yyyy. The could would represent the above expamles as: 12.01.2008 and 12.17.2008
My idea is to just write the day, month and year into seperate columns and then connect the strings in the format mm.dd.yyyy
So far I have tried to withdraw the year and the month just by their position in the string (see code and example below). But this does not work with the days as there are two cases: the day has either one or two digits.
My idea is to just use a regular expression It is basically the case backslash one or two digits and a backslash. But I do not know how I can express this as a regular expression.
Or is there totally different approach which is much simpler?
Thank you for the help in advance! I am sure that there is a way to do that with regular expressions. But I am also grateful for totally different approaches.
import pandas as pd
# example data frame with dates in the format mm/d/yyyy and mm/dd/yyyy
df = pd.DataFrame({'date' : ['12/1/2008','12/5/2008','12/10/2008','12/17/2008']})
# withdraw month
df['month'] = df['date'].str[:2]
# withdraw year
df['year'] = df['date'].str[-4:]
# withdraw day - this is my problem
df[day] = df['day'] = df['date'].str.extract(r'[\]\d*')
# generate string with dates in the format mm/dd/yyyy
df['date_new'] = df['month'] + '.' df['day'] + '.' + df['year']
From the code of df['day'] I get the following error: error: unterminated character set at position 0
I think you are looking for this:
df['date'] = pd.to_datetime(df['date'])
df['date'] = df['date'].dt.strftime('%m.%d.%Y')
Output:
date
0 12.01.2008
1 12.05.2008
2 12.10.2008
3 12.17.2008
Another thing to bring to your attention if you want to extract days, months, years or so, pandas has a special dt functionality for datetime types, hence, you need to convert your column first into that type.
You can access days and months like this:
df['date'] = pd.to_datetime(df['date'])
df['month'] = df['date'].dt.month
df['day'] = df['date'].dt.day
df['year'] = df['date'].dt.year
You will get something like:
date month day year
0 2008-12-01 12 1 2008
1 2008-12-05 12 5 2008
2 2008-12-10 12 10 2008
3 2008-12-17 12 17 2008
I have a question. I have a field in my SAS dataset that has a mixture of datetime and date variables.
The field is PST_DT and is Type: Numeric. Group: Date. Format: Date9. Length: 8.
Some values look like this:
PST_DT
8/22/2018 11:59:59 PM
8/22/2018
How can I turn just the datetime values in date format? I want all the values to be in date format.
Thanks.
The question does not make much sense as a date variable (number of days since 1960) cannot have a time component at all, much less have it selectively.
If you have a DATETIME value (number of seconds since 1960) and want to convert it to a date value use the datepart() function. And attach a date format so that the value displays in a human friendly way.
pst_date = datepart(pst_dt);
format pst_date yymmdd10. ;
If you have instead a character string then use the ANYDTDTE. informat to convert it to a date value.
pst_date = input(pst_dt,anydtdte40.);
format pst_date yymmdd10. ;
I have a date field coming form source paid_date and i want it to convert as
trunc(next_day(sysdate-1,'MON')).. I need to get the NEXT_DAY here and the filed data type is date-time.
Please share your inputs.
Unfortunately, as of now, there is no NEXT_DAY equivalent in Informatica. So you have to calculate it like this in expression.
TRUNC(
ADD_TO_DATE(
SYSDATE,
'DD',
(9 - TO_FLOAT(TO_CHAR(SYSDATE,'D')))%7
)
)
Explanation:
(9 - TO_FLOAT(TO_CHAR(SYSDATE,'D')))%7 - Calculates the number of days till next Monday.
ADD_TO_DATE(SYSDATE,'DD',...) - Adds the above no. of days to the input date
In this case you can use Add_To_Date function. Using this function you can get your exact date or month or year.
Formats of defining date,
Date – DD, DDD, DY and DAY
Month – MM, MON and MONTH
Year – YY, YYY and YYYY
Hour – HH, HH12 and HH24
Minute – MI
Seconds - SS
Syntax : ADD_TO_DATE (date_column, format, value)
Example: ADD_TO_DATE (Date, ‘DD’, 10)
Result:
10/01/2016 - 20/01/2016
As the format is provided as ‘DD’ and value as 10, the dates are displayed by increasing 10 days. This logics stands for date, month, year, minute, hour or seconds whatever defined in the syntax. To decrease the date value just add negative number (-10).
For you to get next day, just define
ADD_TO_DATE (Your column, ‘DD’, 1).
For more details on informatica just visit my blog,
http://etlinfromatica.wordpress.com/
I have a model called Post with an attribute date.
I want to filter the queryset so that I retrieve posts only in the next x months. e.g. next 3 months
from datetime import datetime
import calendar
def add_months(sourcedate, months):
month = sourcedate.month - 1 + months
year = sourcedate.year + month / 12
month = month % 12 + 1
day = min(sourcedate.day, calendar.monthrange(year,month)[1])
return datetime.date(year, month, day)
today = datetime.today()
table_name.objects.filter(date__range=[str(today.date()), str(add_months(today, 3).date())])
Reference
- How to increment datetime by custom months in python without using library
- Django database query: How to filter objects by date range?
Take a look at range. Build the date and pass to the QuerySet. Relevant SO question for reference.