Making date regular in timetable
조회 수: 9 (최근 30일)
이전 댓글 표시
Why are the date sequences on a quarterly frequency irregular? Is there a way to make them regular?
% creating sequence of dates with quarterly frequency
enDt = datetime('31/12/2018');
stDt = enDt - calquarters(24);
Time = stDt:calquarters(1):enDt;
%%
% creating random timetable
Var1 = rand(length(Time),1);
Var2 = rand(length(Time),1);
Var3 = rand(length(Time),1);
Var4 = rand(length(Time),1);
TT = timetable(Time,Var1,Var2,Var3,Var4);
%%
t = TT.Time;
missIDX = ismissing( TT ); % nothing missing
all(sum(missIDX))
% Question 1: Why is TT not regular???
isReg = isregular( TT, 'quarters' );
% Date vector is all showing consistent 1 quarter duration
dt = caldiff( t, 'quarters' );
% Question 2: Why do the dates all shift when i synchronize??? I would like
% the dates to remain as the date sequence given above.
TT2 = synchronize( TT, 'quarterly' );
댓글 수: 0
채택된 답변
dpb
2019년 3월 9일
편집: dpb
2019년 3월 10일
Q1. Because you created a timetable with the time vector that is last day of each calendar quarter month over a period of years. A timetable uses actual time and there aren't the same number of days in every calendar quarter--there can't be because neither 365 nor 366 is exactly divisible by 4.
For your TT
>> diff(TT.Time)
ans =
24×1 duration array
2160:00:00
2184:00:00
2208:00:00
2208:00:00
2160:00:00
...
showing the differing hours/days in each quarter. They simply are NOT the same actual time spans; a "quarter" is a division of an equal number of months, but all months are not created equal.
caldiff has the logic built into it to return equal intervals of the type requested accounting for the actual differences in absolute time. Consequently, it returns the expected result that matches the result of calquarters (as it should). But neither are consistent in terms of absolute time.
synchronize uses the beginning of the time period and you're defined your starting point as the last day of the quarter, not the first. I'm suspecting that's at least part of the issue there.
The first two parts are clear and expected; I'm not sure how to resolve the last Q? because I don't fully understand the objective from the example. If you showed the real application, probably somebody here could help resolve your difficulties.
댓글 수: 13
michal.markun
2023년 1월 5일
Hi, the discussion is long ago, but it's still funny, sort of, that I 'self-discovered', just like Jeff did, all the above issues again on my own.
I use Matlab R2019b and maybe the issue has already been solved, but at this point my conclusion is that I don't see the point in keeping the precise dates in timetables if the format is specified as mytimetable.Time.Format='yyyyQQQ'.
I expected that in this case Matlab abstract from a precise day of a quarter, so that I can synchronize two timetables read from Excel files where dates formats were different, eg. 30.06.1992 and 1992Q2. Instead I first had to dateshift the fist timetable to the start of quarter. No big deal, but it took me some time to figure it out, and still seems a bit illogical.
Steven Lord
2023년 1월 5일
@michal.markun, changing the Format property of a datetime array does not actually change the contents of the array. All it does is change how the array is displayed. If we did as you suggested and actually changed the data, x1 and x2 in the following example would not be the same and I think a lot of people would consider that a bug.
x1 = datetime('now')
F = x1.Format % Store the original format
x1.Format = 'yyyyQQQ'
x2 = x1
x1.Format = F
x2 % Keeps its Format even though x1's Format changed
isequal(x1, x2)
x3 = datetime('2023Q1', 'InputFormat', 'yyyyQQQ')
isequal(x1, x3) % false
x3.Format = 'yyyyQQQ' % But its display matches x2
isequal(x2, x3) % also false
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!