First discrete difference of Series elements. Calculates the difference of a Series element compared with another element in the Series (default is element in previous row). Parameters ---------- periods : int, default 1 Periods to shift
(self, periods: int = 1)
| 2834 | return result |
| 2835 | |
| 2836 | def diff(self, periods: int = 1) -> Series: |
| 2837 | """ |
| 2838 | First discrete difference of Series elements. |
| 2839 | |
| 2840 | Calculates the difference of a Series element compared with another |
| 2841 | element in the Series (default is element in previous row). |
| 2842 | |
| 2843 | Parameters |
| 2844 | ---------- |
| 2845 | periods : int, default 1 |
| 2846 | Periods to shift for calculating difference, accepts negative |
| 2847 | values. |
| 2848 | |
| 2849 | Returns |
| 2850 | ------- |
| 2851 | Series |
| 2852 | First differences of the Series. |
| 2853 | |
| 2854 | See Also |
| 2855 | -------- |
| 2856 | Series.pct_change: Percent change over given number of periods. |
| 2857 | Series.shift: Shift index by desired number of periods with an |
| 2858 | optional time freq. |
| 2859 | DataFrame.diff: First discrete difference of object. |
| 2860 | |
| 2861 | Notes |
| 2862 | ----- |
| 2863 | For boolean dtypes, this uses :meth:`operator.xor` rather than |
| 2864 | :meth:`operator.sub`. |
| 2865 | The result is calculated according to current dtype in Series, |
| 2866 | however dtype of the result is always float64. |
| 2867 | |
| 2868 | Examples |
| 2869 | -------- |
| 2870 | |
| 2871 | Difference with previous row |
| 2872 | |
| 2873 | >>> s = pd.Series([1, 1, 2, 3, 5, 8]) |
| 2874 | >>> s.diff() |
| 2875 | 0 NaN |
| 2876 | 1 0.0 |
| 2877 | 2 1.0 |
| 2878 | 3 1.0 |
| 2879 | 4 2.0 |
| 2880 | 5 3.0 |
| 2881 | dtype: float64 |
| 2882 | |
| 2883 | Difference with 3rd previous row |
| 2884 | |
| 2885 | >>> s.diff(periods=3) |
| 2886 | 0 NaN |
| 2887 | 1 NaN |
| 2888 | 2 NaN |
| 2889 | 3 2.0 |
| 2890 | 4 4.0 |
| 2891 | 5 6.0 |
| 2892 | dtype: float64 |
| 2893 |