# Periods / shifts argument for the diff function

**URL:** <https://discourse.julialang.org/t/periods-shifts-argument-for-the-diff-function/63701>\
**Category:** General Usage\
**Tags:** base\
**Created:** [June 28, 2021, 3:22pm UTC](https://discourse.julialang.org/t/periods-shifts-argument-for-the-diff-function/63701 "2021-06-28T15:22:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![scimas](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@scimas](https://discourse.julialang.org/u/scimas)\
**Post date:** [June 28, 2021, 3:22pm UTC](https://discourse.julialang.org/t/periods-shifts-argument-for-the-diff-function/63701/1 "2021-06-28T15:22:37Z")

</div>

The [diff function](https://docs.julialang.org/en/v1/base/arrays/#Base.diff) doesn’t have a periods / shifts argument, similar to the equivalent [pandas function](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.diff.html?highlight=diff#pandas.DataFrame.diff). But looking at the julia source code, it seems it would be easy to add it without breaking any existing code.

So before I go create an issue and a PR, is there any reason this is not already implemented (beyond that no one has gotten around to doing it)? Is there a better way to achieve the same functionality?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [June 28, 2021, 3:41pm UTC](https://discourse.julialang.org/t/periods-shifts-argument-for-the-diff-function/63701/2 "2021-06-28T15:41:08Z")

</div>

why? feature-bloated pandas don’t even have it, why should we? (pandas is feature-bloated because anything user have to DIY put together is significantly slower)

---

<div class="post-metadata">

**Author:** ![scimas](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@scimas](https://discourse.julialang.org/u/scimas)\
**Post date:** [June 28, 2021, 3:48pm UTC](https://discourse.julialang.org/t/periods-shifts-argument-for-the-diff-function/63701/3 "2021-06-28T15:48:02Z")

</div>

> [@jling](#):
>
> feature-bloated pandas don’t even have it

No comments on pandas being feature bloated, but what do you mean by “pandas don’t even have it”? I have literally linked to their documentation showing that they do have that argument and functionality.

> [@jling](#):
>
> why?

Because it’s useful for things like time series analysis. Single step and other period differencing is often useful before doing autocorrelation analysis. Single step difference is what diff does, larger periods are not supported at the moment.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [June 28, 2021, 3:53pm UTC](https://discourse.julialang.org/t/periods-shifts-argument-for-the-diff-function/63701/4 "2021-06-28T15:53:44Z")

</div>

> [@scimas](#):
>
> similar to the equivalent

`diff` doesn’t have shift

---

<div class="post-metadata">

**Author:** ![scimas](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@scimas](https://discourse.julialang.org/u/scimas)\
**Post date:** [June 28, 2021, 4:06pm UTC](https://discourse.julialang.org/t/periods-shifts-argument-for-the-diff-function/63701/5 "2021-06-28T16:06:56Z")

</div>

It does have the `periods` arg. But periods may not be an intuitive name for the argument when not working with time series data, which is why I have included the `shifts` as an alternative name for the arg. shifts feels much more neutral (shifts=5 read as array shifted by 5 along index).

Also I’m not sure what you’re trying to argue? None of it is really an argument against adding this to the julia function.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [June 28, 2021, 4:10pm UTC](https://discourse.julialang.org/t/periods-shifts-argument-for-the-diff-function/63701/6 "2021-06-28T16:10:17Z")

</div>

> [@scimas](#):
>
> Also I’m not sure what you’re trying to argue?

what should go into a package should not be in `Base`

and sorry, I misunderstood this:

> [@scimas](#):
>
> similar to the equivalent

thought you’re saying Pandas also doesn’t have it but we should add.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [June 28, 2021, 4:14pm UTC](https://discourse.julialang.org/t/periods-shifts-argument-for-the-diff-function/63701/7 "2021-06-28T16:14:01Z")

</div>

Use ShiftedArrays.jl.

```julia
using ShiftedArrays
mydiff(x,n=1) = x - lag(x,n)

```

Note that this returns an object with the same length as `x`, which is not exactly what `diff` does.

---

<div class="post-metadata">

**Author:** ![scimas](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@scimas](https://discourse.julialang.org/u/scimas)\
**Post date:** [June 28, 2021, 4:33pm UTC](https://discourse.julialang.org/t/periods-shifts-argument-for-the-diff-function/63701/8 "2021-06-28T16:33:52Z")

</div>

That does look like almost exactly what I want. Thank you.
