{
  "name": "Difference functions in NumPy and pandas: signatures and output rules",
  "description": "Compares four free Python functions that compute successive differences (numpy.diff, numpy.ediff1d, numpy.gradient, pandas Series.diff) by signature, output shape and handling of the ends, compiled from the official NumPy and pandas reference pages.",
  "page": "https://casrai.org/guides/numpy-diff",
  "publisher": "CASRAI",
  "license": "https://creativecommons.org/licenses/by/4.0/",
  "dateModified": "2026-11-13T05:59:32Z",
  "temporalCoverage": "2026",
  "spatialCoverage": null,
  "columns": [
    {
      "name": "Function",
      "unit": "",
      "description": ""
    },
    {
      "name": "Signature",
      "unit": "",
      "description": ""
    },
    {
      "name": "Output shape relative to input",
      "unit": "",
      "description": ""
    },
    {
      "name": "Handling of ends",
      "unit": "",
      "description": ""
    }
  ],
  "rows": [
    {
      "Function": "numpy.diff",
      "Signature": "numpy.diff(a, n=1, axis=-1, prepend=, append=)",
      "Output shape relative to input": "Same as input except along axis, where it is smaller by n",
      "Handling of ends": "prepend and append add values along the axis before differencing"
    },
    {
      "Function": "numpy.ediff1d",
      "Signature": "numpy.ediff1d(ary, to_end=None, to_begin=None)",
      "Output shape relative to input": "Always 1-D; input is flattened first",
      "Handling of ends": "to_begin and to_end add values at the ends"
    },
    {
      "Function": "numpy.gradient",
      "Signature": "numpy.gradient(f, *varargs, axis=None, edge_order=1)",
      "Output shape relative to input": "Same shape as the input",
      "Handling of ends": "Central differences inside; first- or second-order one-sided differences at the boundaries (edge_order)"
    },
    {
      "Function": "pandas.Series.diff",
      "Signature": "Series.diff(periods=1)",
      "Output shape relative to input": "Same length as the Series",
      "Handling of ends": "Leading periods positions are filled with a missing value"
    }
  ],
  "sources": [
    {
      "title": "numpy.diff",
      "url": "https://numpy.org/doc/stable/reference/generated/numpy.diff.html",
      "accessed": "2026-11-13"
    },
    {
      "title": "numpy.ediff1d",
      "url": "https://numpy.org/doc/stable/reference/generated/numpy.ediff1d.html",
      "accessed": "2026-11-13"
    },
    {
      "title": "numpy.gradient",
      "url": "https://numpy.org/doc/stable/reference/generated/numpy.gradient.html",
      "accessed": "2026-11-13"
    },
    {
      "title": "pandas.Series.diff",
      "url": "https://pandas.pydata.org/docs/reference/api/pandas.Series.diff.html",
      "accessed": "2026-11-13"
    }
  ]
}