Inconsistent Dates in Time Series

6 ビュー (過去 30 日間)
AU
AU 2019 年 5 月 30 日
コメント済み: AU 2019 年 5 月 31 日
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.

採用された回答

Steven Lord
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 件のコメント
AU
AU 2019 年 5 月 30 日
Thank you very much for your detailed explanation. It works now!
Steven Lord
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
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
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.
AU
AU 2019 年 5 月 31 日
Thank you very much Adam!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by