Inconsistent Dates in Time Series
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
AU
el 30 de Mayo de 2019
Comentada: AU
el 31 de Mayo de 2019
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 comentarios
Respuesta aceptada
Steven Lord
el 30 de Mayo de 2019
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 comentarios
Steven Lord
el 30 de Mayo de 2019
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.
Más respuestas (1)
Adam Danz
el 30 de Mayo de 2019
Editada: Adam Danz
el 30 de Mayo de 2019
% 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 comentarios
Adam Danz
el 30 de Mayo de 2019
Editada: Adam Danz
el 30 de Mayo de 2019
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.
Ver también
Categorías
Más información sobre Time Series Objects en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!