Inconsistent Dates in Time Series
조회 수: 1 (최근 30일)
이전 댓글 표시
I have a time series of stock returns with inconsistent dates. Some are in the format dd-mmm-yy and others in the format dd.mm.yyyy. I tried to use the following code to adjust the dates and to get the same date format:
Dates = Dependent_v.Date(:,1);
F = regexp(Dates,'-');
G = cellfun(@isempty,F);
if G = 0
I = datenum(F, 'dd.MM.yyyy');
else
I = datenum(F, 'dd-MMM-yy');
However, I always get the error code: The expression to the left of the equals sign is not a valid target for an assignment.
댓글 수: 0
채택된 답변
Steven Lord
2019년 5월 30일
If possible use datetime instead of datenum. Let's look at two sample dates in different formats.
Dates = {'27-Jun-19', '13.04.2018'};
Try converting all the Dates using the first format.
DT = datetime(Dates, 'InputFormat', 'dd-MMM-yy')
The datetime function didn't know how to convert the second date using that format, so it returned NaT (Not-a-Time, the datetime equivalent of NaN or missing.) Let's identify the dates that could not be converted with the first format by looking for the NaT values.
otherFormat = isnat(DT);
Fill in the NaT values in DT by converting the corresponding elements in Dates using the second format.
DT(otherFormat) = datetime(Dates(otherFormat), 'InputFormat', 'dd.MM.yyyy')
This generalizes if you have a list of potential formats, just preallocate DT using the NaT function then convert the elements in Dates corresponding to NaT using each format in a for loop over the list of formats. Hopefully at each iteration of the loop the number of NaT values in your array decreases, so you'll have fewer and fewer dates to convert each iteration.
By the way, the reason you received that error in your original code is:
if G = 0
The = operator is for assignment, which is not legal as the condition of an if statement. The == operator tests two expressions for equality.
댓글 수: 2
Steven Lord
2019년 5월 30일
Adam Danz called out that "Some of the dates that are in 'dd-mmm-yy' format only have a single digit for the day (example: 1-Dec-11 should be 01-Dec-11) ."
In datetime Format values, using one d describes "Day of the month, using one or two digits" so you'd probably want to use that instead of dd which is "Day of the month using two digits" in your format. The full table of identifiers is in the description of the Format property on the datetime documentation page.
추가 답변 (1개)
Adam Danz
2019년 5월 30일
편집: Adam Danz
2019년 5월 30일
% Create fake data
F = [cellstr(datestr(now-10:now, 'dd-mmm-yy')); cellstr(datestr(now-10:now, 'dd.mm.yyyy'))];
% Identify dates in 'dd-mmm-yy' format
idx = ~cellfun(@isempty,regexp(F, '\d{1,2}-[A-z]{3}-\d{2}'));
% do conversions
I = zeros(size(F));
I(idx) = datenum(F(idx), 'dd-mmm-yy');
I(~idx) = datenum(F(~idx),'dd.mm.yyyy');
댓글 수: 7
Adam Danz
2019년 5월 30일
편집: Adam Danz
2019년 5월 30일
I see what happened. Some of the dates that are in 'dd-mmm-yy' format only have a single digit for the day (example: 1-Dec-11 should be 01-Dec-11) .
The fix is to accept single-digit format within the regular expression:
idx = ~cellfun(@isempty,regexp(F, '\d{1,2}-[A-z]{3}-\d{2}'));
% ^^
I've updated my answer to include single-digit days in that format.
참고 항목
카테고리
Help Center 및 File Exchange에서 Time Series Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!