Repository navigation
grp2idx: accept datetime grouping variables - #522
Merged
Merged
Conversation
A datetime vector is grouped in sorted order, NaT is treated as missing like NaN, and the group names follow the display format. The results match MATLAB R2026b.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
grp2idxrefused datetime input with "'datetime' grouping variable is not supported yet", while MATLAB accepts it. This adds support, following the existing duration path.Changes (inst/Data_Manipulation/grp2idx.m)
uniquefrom datatypes, so they are in sorted order.NaNing, and left out ofgnandgl. This usesisnatin a separate branch, sinceisnanis not defined for datetime. The paths for the other types are unchanged.gnis built withcellstr, as for duration, so it follows the display format.grp2idx.Comparison with MATLAB R2026b
The new tests reproduce MATLAB's output for these cases:
g = [3; 2; 3; NaN; 1], names earliest first,gla 3x1 datetimeFormat'2025-03-01')gall NaN,gn0x1 cell,gl0x1 datetimegldatetimeTimeZonekept onglTesting
test grp2idx: 65 of 65 pass. Seven tests are new and one error test was replaced.grpstats,crosstabanddummyvartests still pass.I also called every public function that uses
grp2idxwith a datetime grouping variable. These now work with dates:grpstats,fitdist('By'),anova,anova1,kruskalwallis,manova1,vartestn,andrewsplot,gplotmatrix,parallelcoords,scatterhist,fitcdiscr,fitcknn,fitcnb,fitcgam,fitcnet,fitcsvm,fitcecoc,fitclinearandfitckernel. For the classifiers,predictreturns datetime labels.Follow-up work
This PR is kept to
grp2idx. In the future, datetime support can be extended to all functions that callgrp2idx. The remaining ones, each a small separate fix:grp2idx:crosstab(crosstab.m:79),dummyvar(dummyvar.m:179),tabulate(tabulate.m:73-76) andmnrfit(mnrfit.m:105-109).fitctreefails intreeNodeStats.m(line 93), which converts class names to text withnum2str.TreeBaggerandfitcensembleare refused by the 'ClassNames' type check inClassificationTree.m(lines 874-882).