Why is day of pandas date_range not what I specified? - python-2.7

Why is the first date not 4/19/1965? Why day 30 instead of 19?
dates = pd.date_range('1965-04-19', freq='6M', periods=3)
dates[0]
Timestamp('1965-04-30 00:00:00', offset='6M')

It looks like the default behavior for freq='M' is MonthEnd(), so
dates = pd.date_range('1965-04-19', periods=3, freq='6M')
dates
DatetimeIndex(['1965-04-30', '1965-10-31', '1966-04-30'], dtype='datetime64[ns]', freq='6M', tz=None)
This can be changed by setting freq = pd.tseries.offsets.DateOffset(months=6).
dates = pd.date_range('1965-04-19', periods=3, freq=pd.tseries.offsets.DateOffset(months=6))
dates
DatetimeIndex(['1965-04-19', '1965-10-19', '1966-04-19'], dtype='datetime64[ns]', freq='<DateOffset: kwds={'months': 6}>', tz=None)

Related

How to format dates for use in SAS?

I am trying to adapt Method 4 in this paper to calculate the duration of many observations, but discounting overlapping dates: https://support.sas.com/resources/papers/proceedings/proceedings/sugi31/048-31.pdf
For example, two rows of observations for subject 101 lasting from 2017-03-02 to 2017-03-16 and 2017-03-04 to 2017-03-17 respectively should return a value of only 16 days.
I am getting an error with the dates being 'Invalid numeric data', though, resulting in later errors. I have tried format startdate yyyymmdd10.; and format stopdate yyyymmdd10.; with no success.
Can anyone help me properly format my dates for use here, or identify any further errors?
Edit: Line 80 refers to do xdate = startdate to stopdate;.
I am still unable to convert or create the date variables as numeric/date values. I have used the following code:
data sasuser.Mdm;
set sasuser.Mdm;
do xdate = input(Startdate,yymmdd10.) to input(stopdate,yymmdd10.);
put xdate= yymmdd10.;
output;
end;
run;
To get this output:
1 data sasuser.Mdm;
2 set sasuser.Mdm;
3 do xdate = input(Startdate,yymmdd10.) to input(stopdate,yymmdd10.);
4 put xdate= yymmdd10.;
5 output;
6 end;
7 run;
xdate=2017-03-02
xdate=2017-03-03
xdate=2017-03-04
xdate=2017-03-05
xdate=2017-03-06
xdate=2017-03-07
xdate=2017-03-08
xdate=2017-03-09
xdate=2017-03-10
xdate=2017-03-11
xdate=2017-03-12
xdate=2017-03-13
xdate=2017-03-14
xdate=2017-03-15
xdate=2017-03-16
xdate=2017-03-04
xdate=2017-03-05
xdate=2017-03-06
xdate=2017-03-07
xdate=2017-03-08
xdate=2017-03-09
xdate=2017-03-10
xdate=2017-03-11
xdate=2017-03-12
xdate=2017-03-13
xdate=2017-03-14
xdate=2017-03-15
xdate=2017-03-16
xdate=2017-03-17
xdate=2017-03-07
xdate=2017-03-08
xdate=2017-03-09
xdate=2017-03-10
xdate=2017-03-11
xdate=2017-03-12
xdate=2017-03-13
xdate=2017-03-14
xdate=2017-03-15
xdate=2017-03-16
xdate=2017-03-17
xdate=2017-03-18
xdate=2017-03-19
xdate=2017-03-20
xdate=2017-03-21
xdate=2017-02-08
xdate=2017-02-09
xdate=2017-02-10
xdate=2017-02-11
xdate=2017-02-12
xdate=2017-02-13
xdate=2017-02-14
xdate=2017-02-15
xdate=2017-02-16
xdate=2017-02-17
xdate=2017-02-18
xdate=2017-02-19
xdate=2017-02-20
xdate=2017-02-21
xdate=2017-02-22
xdate=2017-02-23
xdate=2017-02-24
xdate=2017-02-23
xdate=2017-02-24
xdate=2017-02-25
xdate=2017-02-26
xdate=2017-02-27
xdate=2017-02-28
xdate=2017-03-01
xdate=2017-03-02
xdate=2017-03-03
xdate=2017-03-04
xdate=2017-03-05
xdate=2017-03-06
xdate=2017-03-07
xdate=2017-03-08
xdate=2017-02-26
xdate=2017-02-28
xdate=2017-03-13
xdate=2017-03-17
xdate=2017-03-25
xdate=2017-03-28
xdate=2017-03-23
xdate=2017-03-24
xdate=2017-03-25
xdate=2017-03-26
xdate=2017-03-27
xdate=2017-03-28
xdate=2017-03-29
xdate=2017-03-30
xdate=2017-03-29
xdate=2017-04-03
xdate=2017-04-04
xdate=2017-04-03
xdate=2017-04-04
xdate=2017-04-05
xdate=2017-04-05
xdate=2017-04-06
xdate=2017-04-06
xdate=2017-04-07
xdate=2017-03-25
xdate=2017-03-26
xdate=2017-03-30
xdate=2017-04-01
xdate=2017-04-02
xdate=2017-04-03
xdate=2017-04-04
xdate=2017-04-08
xdate=2017-04-09
xdate=2017-04-10
xdate=2017-04-11
xdate=2017-04-12
xdate=2017-04-12
xdate=2017-04-13
xdate=2017-04-13
xdate=2017-04-14
xdate=2017-04-15
xdate=2017-04-16
xdate=2017-04-17
xdate=2017-04-18
xdate=2017-04-19
xdate=2017-04-20
xdate=2017-04-21
xdate=2017-04-22
xdate=2017-04-19
xdate=2017-04-23
xdate=2017-04-24
xdate=2017-04-25
xdate=2017-04-26
xdate=2017-04-26
xdate=2017-04-27
xdate=2017-04-28
xdate=2017-05-05
xdate=2017-05-06
xdate=2017-05-16
xdate=2017-05-19
xdate=2017-05-20
xdate=2017-05-21
xdate=2017-05-22
xdate=2017-05-19
xdate=2017-05-20
xdate=2017-05-21
xdate=2017-05-22
xdate=2017-05-23
xdate=2017-05-24
xdate=2017-05-25
xdate=2017-05-26
xdate=2017-05-22
xdate=2017-05-23
xdate=2017-05-24
xdate=2017-05-25
xdate=2017-05-26
xdate=2017-05-27
xdate=2017-05-28
xdate=2017-05-29
xdate=2017-05-30
xdate=2017-05-31
xdate=2017-06-01
xdate=2017-06-02
xdate=2017-06-03
xdate=2017-06-04
xdate=2017-06-05
xdate=2017-06-06
xdate=2017-06-07
xdate=2017-06-08
xdate=2017-06-09
xdate=2017-06-10
xdate=2017-06-11
xdate=2017-06-12
xdate=2017-06-13
xdate=2017-06-14
xdate=2017-06-15
xdate=2017-06-16
xdate=2017-06-17
xdate=2017-06-18
xdate=2017-06-19
xdate=2017-06-20
xdate=2017-06-21
xdate=2017-06-22
xdate=2017-06-23
xdate=2017-06-24
xdate=2017-06-25
xdate=2017-06-26
xdate=2017-06-27
xdate=2017-06-28
xdate=2017-06-29
xdate=2017-06-30
xdate=2017-07-01
xdate=2017-07-02
xdate=2017-07-03
xdate=2017-07-04
xdate=2017-07-05
xdate=2017-07-06
xdate=2017-07-07
xdate=2017-07-08
xdate=2017-07-09
xdate=2017-07-10
xdate=2017-07-11
xdate=2017-07-12
xdate=2017-07-13
xdate=2017-07-14
xdate=2017-07-15
xdate=2017-07-16
xdate=2017-07-17
xdate=2017-07-18
xdate=2017-07-19
xdate=2017-07-20
xdate=2017-07-21
xdate=2017-07-22
xdate=2017-07-23
xdate=2017-07-24
xdate=2017-07-25
xdate=2017-07-26
xdate=2017-07-27
xdate=2017-07-28
xdate=2017-07-29
xdate=2017-07-30
xdate=2017-07-31
xdate=2017-08-01
xdate=2017-08-02
xdate=2017-08-03
xdate=2017-08-04
xdate=2017-08-05
xdate=2017-08-06
xdate=2017-08-07
xdate=2017-08-08
xdate=2017-08-09
xdate=2017-08-10
xdate=2017-08-11
xdate=2017-08-12
xdate=2017-08-13
xdate=2017-08-14
xdate=2017-08-15
xdate=2017-08-16
xdate=2017-08-17
xdate=2017-08-18
xdate=2017-08-19
xdate=2017-08-20
xdate=2017-08-21
xdate=2017-08-22
xdate=2017-08-23
xdate=2017-08-24
xdate=2017-08-25
xdate=2017-08-26
xdate=2017-08-27
xdate=2017-08-28
xdate=2017-08-29
xdate=2017-08-30
xdate=2017-08-31
xdate=2017-09-01
xdate=2017-05-27
xdate=2017-05-28
xdate=2017-05-29
xdate=2017-05-30
xdate=2017-05-31
xdate=2017-06-01
xdate=2017-06-02
xdate=2017-06-03
xdate=2017-06-04
xdate=2017-06-05
xdate=2017-06-06
xdate=2017-06-07
xdate=2017-06-08
xdate=2017-06-09
xdate=2017-06-10
xdate=2017-06-11
xdate=2017-06-12
xdate=2017-06-13
xdate=2017-06-14
xdate=2017-06-15
xdate=2017-06-16
xdate=2017-06-17
xdate=2017-06-18
xdate=2017-06-19
xdate=2017-06-20
xdate=2017-06-21
xdate=2017-06-22
xdate=2017-06-23
xdate=2017-06-24
xdate=2017-06-25
xdate=2017-06-26
xdate=2017-06-27
xdate=2017-06-28
xdate=2017-06-29
xdate=2017-06-30
xdate=2017-07-01
xdate=2017-07-02
xdate=2017-07-03
xdate=2017-07-04
xdate=2017-07-05
xdate=2017-07-06
xdate=2017-07-07
xdate=2017-07-08
xdate=2017-07-09
xdate=2017-07-10
xdate=2017-07-11
xdate=2017-07-12
xdate=2017-07-13
xdate=2017-07-14
xdate=2017-07-15
xdate=2017-07-16
xdate=2017-07-17
xdate=2017-07-18
xdate=2017-07-19
xdate=2017-07-20
xdate=2017-07-21
xdate=2017-07-22
xdate=2017-07-23
xdate=2017-07-24
xdate=2017-07-25
xdate=2017-07-26
xdate=2017-07-27
xdate=2017-07-28
xdate=2017-07-29
xdate=2017-07-30
xdate=2017-07-31
xdate=2017-08-01
xdate=2017-08-02
xdate=2017-08-03
xdate=2017-08-04
xdate=2017-08-05
xdate=2017-08-06
xdate=2017-08-07
xdate=2017-08-08
xdate=2017-08-09
xdate=2017-08-10
xdate=2017-08-11
xdate=2017-08-12
xdate=2017-08-13
xdate=2017-08-14
xdate=2017-08-15
xdate=2017-08-16
xdate=2017-08-17
xdate=2017-08-18
xdate=2017-08-19
xdate=2017-08-20
xdate=2017-08-21
xdate=2017-08-22
xdate=2017-08-23
xdate=2017-08-24
xdate=2017-08-25
xdate=2017-08-26
xdate=2017-08-27
xdate=2017-08-28
xdate=2017-08-29
xdate=2017-08-30
xdate=2017-08-31
xdate=2017-09-01
xdate=2017-06-14
xdate=2017-06-15
xdate=2017-06-16
xdate=2017-06-17
xdate=2017-06-18
xdate=2017-06-19
xdate=2017-06-20
xdate=2017-06-21
xdate=2017-06-22
xdate=2017-06-23
xdate=2017-06-24
xdate=2017-06-25
xdate=2017-06-26
xdate=2017-06-27
xdate=2017-06-28
xdate=2017-06-29
xdate=2017-06-14
xdate=2017-06-15
xdate=2017-06-16
xdate=2017-06-17
xdate=2017-06-18
xdate=2017-06-19
xdate=2017-06-20
xdate=2017-06-21
xdate=2017-06-22
xdate=2017-06-23
xdate=2017-06-24
xdate=2017-06-25
xdate=2017-06-26
xdate=2017-06-27
xdate=2017-06-28
xdate=2017-06-29
xdate=2017-03-27
xdate=2017-04-02
xdate=2017-04-07
xdate=2017-04-08
xdate=2017-04-09
xdate=2017-04-13
xdate=2017-04-14
xdate=2017-04-15
xdate=2017-04-16
xdate=2017-04-17
xdate=2017-04-19
xdate=2017-04-20
xdate=2017-04-21
xdate=2017-04-22
xdate=2017-04-23
xdate=2017-04-24
xdate=2017-04-20
xdate=2017-04-21
xdate=2017-04-22
xdate=2017-04-23
xdate=2017-04-24
xdate=2017-04-25
xdate=2017-04-26
xdate=2017-04-27
xdate=2017-04-28
xdate=2017-04-29
xdate=2017-04-30
xdate=2017-05-01
xdate=2017-05-02
xdate=2017-04-24
xdate=2017-04-25
xdate=2017-04-26
xdate=2017-04-27
xdate=2017-04-28
xdate=2017-04-29
xdate=2017-04-30
xdate=2017-05-01
xdate=2017-05-02
xdate=2017-05-03
xdate=2017-05-04
xdate=2017-05-05
xdate=2017-05-06
xdate=2017-05-07
xdate=2017-05-08
xdate=2017-05-09
xdate=2017-05-10
xdate=2017-05-11
xdate=2017-05-12
xdate=2017-05-13
xdate=2017-05-14
xdate=2017-05-15
xdate=2017-05-16
ERROR: Invalid DO loop control information, either the INITIAL or TO expression is missing or the
BY expression is missing, zero, or invalid.
SUBJID=106 KEY=106-9 OBS=9 TOTAL=12 STARTDATE=2017-04-25 STOPDATE= CLASS=Steroid / Diuretic
xdate=20934 _ERROR_=1 _N_=52
NOTE: The SAS System stopped processing this step because of errors.
NOTE: There were 52 observations read from the data set SASUSER.MDM.
WARNING: The data set SASUSER.MDM may be incomplete. When this step was stopped there were 431
observations and 8 variables.
WARNING: Data set SASUSER.MDM was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
real time 0.38 seconds
cpu time 0.29 seconds```
I don't understand why input doesn't appear to be working. Dates are still listed as character strings under column attributes. The do part also isn't working as intended. I'd be grateful for any further guidance.
Do not use the same name in the DATA and SET statement. Then you're always having to rebuild from the start.
Convert your start and stop date to SAS dates
Remove PUT
Add formats to see them displayed as desired
Drop old variables to avoid confusion.
Your two code steps, the data step and SQL do not appear related. Not sure why you would even need a list of dates for intervals or anything. There are much better ways to calculate an overlap. I think you're putting us through an xy problem where it would be significantly easier to show us what you're attempting to do and people would be able to provide a much better solution.
data sasuser.Mdm2; /*1*/
set sasuser.Mdm;
/*2*/
start_date = input(startdate, yymmdd10.);
end_date = input(stopdate, yymmdd10.);
do xdate = start_date to stop_date;
output; /*3*/
end;
/*4*/
format start_date end_date xDate yymmdd10.;
/*5*/
drop startdate stopdate;
run;
*check;
proc contents data=sasuser.mdm2;
run;
EDIT: Also, if you had some sort of grouping variable to indicate that these were part of the same episode you could then just take the min/max of the dates and subtract them to get the interval duration for starters. Grouping via a data step is trivial.
data want;
set have;
by id;
retain episode;
start_date = input(start_date, yymmdd10.);
end_date = input(stopdate, yymmdd10.);
prev_stop_date = lag(stopDate);
if first.id then do;
episode = 0;
call missing(prev_stop_date);
end;
if not (start_date <=prev_stop_date <= end_date) then episode+1;
*could add in logic to calculate dates and durations as well depending....;
run;
It sounds like your SAS log is complaining about this statement.
do xdate=startdate to stopdate;
Because STARTDATE and STOPDATE are character strings instead of dates.
Make sure to create your date values as dates instead of character strings.
Tom's correct, of course, the startdate and stopdate seem to be characters.
To properly use this, do something like this (only the do loop is relevant for you, the rest is to show it working):
data _null_;
startdate = '2017-03-02';
stopdate = '2017-03-16';
do xdate = input(Startdate,yymmdd10.) to input(stopdate,yymmdd10.);
put xdate= yymmdd10.; *just put to the log to see what you are getting;
end;
run;
input will convert the text to a numeric value. Do realize you have to format that xdate as a date format if you want to be able to view it - if you're just using it as an input, though, you can leave the formatting off.

Quantlib; how to use PiecewiseFlatForward

I am using the function PiecewiseFlatForward to build a curve and I am using it by setting settlementDate as an "absolute" starting date, like PiecewiseFlatForward(settlementDate, swapHelpers, Actual360()), so that the curve should be unaffected by a resetting of the Settings.instance().evaluationDate. If I change Settings.instance().evaluationDate to a date in the past with respect to the original settlementDate, the reference date of the curve stays the same, but I get an error when trying to retrieve the forward rates (unlike when using the ForwardCurve function). Here the code, where after resetting the evaluation to a past date the same forwardRate function does not work (RuntimeError: 1st iteration: failed at 1st alive instrument, pillar March 16th, 2020, maturity March 16th, 2020, reference date November 27th, 2019: 2nd leg: negative time (-0.2) given):
calendar = TARGET()
todaysDate = Date(25, November, 2019)
Settings.instance().evaluationDate = todaysDate
settlementDate = Date(27, November, 2019)
calendar = TARGET()
swaps = {
(6,Months): 0.028067,
(12,Months): 0.030768,
(18,Months): 0.029352,
(24,Months): 0.028648,
(30,Months): 0.028532,
(36,Months): 0.028480,
(42,Months): 0.028420,
(48,Months): 0.028394,
(54,Months): 0.028382,
(60,Months): 0.028387}
fixedLegFrequency = Semiannual
fixedLegDayCounter = Thirty360()
fixedLegAdjustment = Unadjusted
swapHelpers = [ SwapRateHelper(QuoteHandle(SimpleQuote(swaps[(n,unit)])),
Period(n,unit), calendar,
fixedLegFrequency, fixedLegAdjustment,
fixedLegDayCounter, USDLibor(Period(6,Months)))
for n, unit in swaps.keys() ]
depoSwapCurve = PiecewiseFlatForward(settlementDate, swapHelpers, Actual360())
dates = [ spot+Period(i*6,Months) for i in range(0, len(swaps)) ]
rates = [ depoSwapCurve.forwardRate(d, USDLibor(Period(6,Months)).maturityDate(d), Actual360(), Simple).rate()
for d in dates ]
print rates
Settings.instance().evaluationDate = Date(12, September, 2019)
rates2 = [ depoSwapCurve.forwardRate(d, USDLibor(Period(6,Months)).maturityDate(d), Actual360(), Simple).rate()
for d in dates ]
print rates2
Is there a way to use the .forwardRate function without having to care about the evaluation date when the function used to build the curve is the PiecewiseFlatForward?
Are you sure there's nothing missing in the code you posted?
After correcting for the error NameError: name 'spot' is not defined (because spot is not defined in your code) with:
spot = calendar.advance(todaysDate, 2, Days)
it seems to work fine...

How to validate dates in Hash format in MySQL or C++

I need to validate dates in Hash (SHA256) format in MySQL or C++.
For example: date1 < date2 or date1 > date2.
I have this query in MySQL:
SELECT SHA1(CURDATE()) -->'2017-09-06'
and:
SELECT SHA1('2017-09-06') --> '34152f3661d73490ac89b0fe15cb3170aac06bb8'
SELECT SHA1('2017-09-07') --> '0b10f03fb245a6486d6ab5b25a2f050bf87093a5'
But, if I use:
SELECT IF (SHA1('2017-09-06') <= SHA1('2017-09-07') ,'True','False') AS Test;
the result is False, therefore, incorrect!
Don't know why you'd do this, but here's a solution for you.
Here's some assumptions.
You were given two date hashes (two inputs)
You know exactly how the date hashes were created (EG: always in the format of YYYY-MM-DD)
You know the range of your target date (So you can reverse the hash back to the original date)
So in psudocode, you'd do something like this.
date getDateFromHash(string inputHash) {
date startDate = '1911-01-01';
date endDate = '2017-12-31';
for(date checkDate = startDate; checkDate < endDate; checkDate + 1 day) {
if (sha1(checkDate) = inputHash) { return checkDate }
}
return null;
}
date firstDate = getDateFromHash("0b10f03fb245a6486d6ab5b25a2f050bf87093a5"); // 2017-09-07
date secondDate = getDateFromHash("34152f3661d73490ac89b0fe15cb3170aac06bb8"); // 2017-09-06
if (firstDate > secondDate) {
return true;
} else {
return false;
} // compare using default date operators
But honestly speaking, like the comment says, there's no point of hashing date values. Hence why I'm only dropping a quich psudo code for you just to point you in the right direction, if you seriously want to tread down this path.

What is the proper date format when converting String to Date in Swift

The date format that I'm passing my DateFormatter is not working.
Why is the year appearing as the first component of the date when I specify month in the dateFormat? Why is my am/pm marker in the dateFormat being ignored? Finally, why or how do I correct for time zone? I didn't specify, yet a different time zone has been used.
Thanks!
if let publishDateString = post["publishDate"] as? String {
print("publishDateString is \(publishDateString)") // 2/17/2016 2:49:00 PM
let myDateFormatter = DateFormatter()
myDateFormatter.dateFormat = "M/d/yyyy h:mm:ss a"
let dateFromString = myDateFormatter.date(from: publishDateString)!
print("My date from string is \(dateFromString)") // 2016-02-17 19:49:00 +0000
}

boost::gregorian::date does not have `set-type` functions?

For example I have a date object:
boost::gregorian::date date1(2013,1,31);
Now I want to change the day to 1. How to set day to 1?
Date types are immutable, apart from assignment, so you need to make a new date:
date1 = boost::gregorian::date(date1.year(), date1.month(), 1);