Skip to content

grp2idx: accept datetime grouping variables - #522

Merged
pr0m1th3as merged 1 commit into
gnu-octave:mainfrom
dheerchheda77:grp2idx-datetime
Oct 16, 2026
Merged

pr0m1th3as merged 1 commit into
gnu-octave:mainfrom
dheerchheda77:grp2idx-datetime

Conversation

@dheerchheda77

Copy link
Copy Markdown
Contributor

grp2idx refused 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)

  • A datetime vector is accepted; a datetime matrix is refused with "'datetime' grouping variable must be a vector.", as for the other types.
  • Groups are numbered by unique from datatypes, so they are in sorted order.
  • NaT is treated as missing: NaN in g, and left out of gn and gl. This uses isnat in a separate branch, since isnan is not defined for datetime. The paths for the other types are unchanged.
  • gn is built with cellstr, as for duration, so it follows the display format.
  • Help text: lists datetime, states that datetime and duration are numbered in sorted order (duration already was, but the text said first appearance), and mentions NaT as missing.
  • doc-cache regenerated for grp2idx.

Comparison with MATLAB R2026b

The new tests reproduce MATLAB's output for these cases:

Case MATLAB R2026b and Octave
Dates with a repeat and one NaT g = [3; 2; 3; NaN; 1], names earliest first, gl a 3x1 datetime
Custom Format names follow the format ('2025-03-01')
Same day, different times separate groups, names include the time
Row vector input all outputs are columns
All NaT g all NaN, gn 0x1 cell, gl 0x1 datetime
Empty all outputs 0x1, gl datetime
Datetime matrix refused
Time zone one group, TimeZone kept on gl

Testing

  • Octave 11.3.0, datatypes 1.5.0.
  • test grp2idx: 65 of 65 pass. Seven tests are new and one error test was replaced.
  • grpstats, crosstab and dummyvar tests still pass.

I also called every public function that uses grp2idx with 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, fitclinear and fitckernel. For the classifiers, predict returns datetime labels.

Follow-up work

This PR is kept to grp2idx. In the future, datetime support can be extended to all functions that call grp2idx. The remaining ones, each a small separate fix:

  • Type checks that refuse datetime before calling grp2idx: crosstab (crosstab.m:79), dummyvar (dummyvar.m:179), tabulate (tabulate.m:73-76) and mnrfit (mnrfit.m:105-109).
  • Classification trees: fitctree fails in treeNodeStats.m (line 93), which converts class names to text with num2str. TreeBagger and fitcensemble are refused by the 'ClassNames' type check in ClassificationTree.m (lines 874-882).

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants