Skip to content

crosstab, tabulate: accept datetime and duration inputs - #525

Merged
pr0m1th3as merged 1 commit into
gnu-octave:mainfrom
dheerchheda77:datetime-crosstab-tabulate
Oct 17, 2026
Merged

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

Conversation

@dheerchheda77

Copy link
Copy Markdown
Contributor

Follow-up to #522. crosstab and tabulate refused datetime and duration vectors with their own type checks before reaching grp2idx, while MATLAB accepts both in both functions.

Changes

  • crosstab: datetime and duration inputs take the existing path for text and categorical inputs, through grp2idx, so labels are the group names and missing values (NaT, NaN) are not counted. A datetime or duration matrix is refused with the existing "must be vectors" error.
  • tabulate: datetime and duration are added to the accepted types and to the error message. A new branch drops missing values with ismissing and numbers the groups with grp2idx, in sorted order, returning the same 3-column cell table as the other non-numeric types.
  • Help text of both functions lists the new types; tabulate also states that NaT and NaN durations are ignored and that the values are sorted.
  • doc-cache entries regenerated for both functions.

Comparison with MATLAB R2026b

Data: three dates repeated six times with one NaT, against 1, 2 and 3 hours.

Call MATLAB R2026b and Octave
[t, chi2, p, labels] = crosstab (d, h) t = [0 6 0; 5 0 0; 0 0 6], chi2 = 34, p = 7.4519e-07, labels '01-Mar-2025' ... and '1 hr' ...
tabulate (d) sorted dates, counts 6, 5, 6, percents 35.2941, 29.4118, 35.2941 (NaT left out of the total)
tabulate (h) '1 hr', '2 hr', '3 hr', counts 6, 6, 6, percents 33.3333

Testing

Octave 11.3.0, datatypes 1.5.0:

  • crosstab: 34 of 34 pass, 3 tests new.
  • tabulate: 35 of 35 pass, 9 tests new. Edge cases cover a NaN duration, row input, all-NaT and empty input, printed output, and matrix input.
  • grp2idx and knnsearch (the other caller of crosstab) still pass.

Not included

Other functions where MATLAB R2026b refuses datetime are left alone: dummyvar, mnrfit, and fitctree / TreeBagger / fitcensemble.

Datetime and duration vectors are grouped through grp2idx, in sorted order, and missing values (NaT, NaN) are not counted.  The results match MATLAB R2026b.
@pr0m1th3as
pr0m1th3as merged commit 75e951b into gnu-octave:main Oct 17, 2026
8 checks passed
pr0m1th3as added a commit that referenced this pull request Oct 17, 2026
…w-up to #525

  * inst/Data_Manipulation/grp2idx.m: Document that a NaN duration is
  missing and that MATLAB makes it a group of its own.

  * inst/Data_Manipulation/crosstab.m: Likewise.  Write each numeric
  label on its own, so none is padded and an empty input has none.
  Convert numeric input to double, so an integer type beside other
  numbers no longer raises an error.  Add tests.

  * inst/Descriptive_Statistics/tabulate.m: Document that a NaN duration
  is missing and the empty table for an all-missing datetime or duration
  vector.  Handle datetime and duration in the branch for strings.  Add
  a test.
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