Returns a new dataset with the first `n` values of each array for the specified dimension(s). Parameters ---------- indexers : dict or int, default: 5 A dict with keys matching dimensions and integer values `n` or a single integer `n` applied
(
self,
indexers: Mapping[Any, int] | int | None = None,
**indexers_kwargs: Any,
)
| 3068 | return shuffled |
| 3069 | |
| 3070 | def head( |
| 3071 | self, |
| 3072 | indexers: Mapping[Any, int] | int | None = None, |
| 3073 | **indexers_kwargs: Any, |
| 3074 | ) -> Self: |
| 3075 | """Returns a new dataset with the first `n` values of each array |
| 3076 | for the specified dimension(s). |
| 3077 | |
| 3078 | Parameters |
| 3079 | ---------- |
| 3080 | indexers : dict or int, default: 5 |
| 3081 | A dict with keys matching dimensions and integer values `n` |
| 3082 | or a single integer `n` applied over all dimensions. |
| 3083 | One of indexers or indexers_kwargs must be provided. |
| 3084 | **indexers_kwargs : {dim: n, ...}, optional |
| 3085 | The keyword arguments form of ``indexers``. |
| 3086 | One of indexers or indexers_kwargs must be provided. |
| 3087 | |
| 3088 | Examples |
| 3089 | -------- |
| 3090 | >>> dates = pd.date_range(start="2023-01-01", periods=5) |
| 3091 | >>> pageviews = [1200, 1500, 900, 1800, 2000] |
| 3092 | >>> visitors = [800, 1000, 600, 1200, 1500] |
| 3093 | >>> dataset = xr.Dataset( |
| 3094 | ... { |
| 3095 | ... "pageviews": (("date"), pageviews), |
| 3096 | ... "visitors": (("date"), visitors), |
| 3097 | ... }, |
| 3098 | ... coords={"date": dates}, |
| 3099 | ... ) |
| 3100 | >>> busiest_days = dataset.sortby("pageviews", ascending=False) |
| 3101 | >>> busiest_days.head() |
| 3102 | <xarray.Dataset> Size: 120B |
| 3103 | Dimensions: (date: 5) |
| 3104 | Coordinates: |
| 3105 | * date (date) datetime64[us] 40B 2023-01-05 2023-01-04 ... 2023-01-03 |
| 3106 | Data variables: |
| 3107 | pageviews (date) int64 40B 2000 1800 1500 1200 900 |
| 3108 | visitors (date) int64 40B 1500 1200 1000 800 600 |
| 3109 | |
| 3110 | # Retrieve the 3 most busiest days in terms of pageviews |
| 3111 | |
| 3112 | >>> busiest_days.head(3) |
| 3113 | <xarray.Dataset> Size: 72B |
| 3114 | Dimensions: (date: 3) |
| 3115 | Coordinates: |
| 3116 | * date (date) datetime64[us] 24B 2023-01-05 2023-01-04 2023-01-02 |
| 3117 | Data variables: |
| 3118 | pageviews (date) int64 24B 2000 1800 1500 |
| 3119 | visitors (date) int64 24B 1500 1200 1000 |
| 3120 | |
| 3121 | # Using a dictionary to specify the number of elements for specific dimensions |
| 3122 | |
| 3123 | >>> busiest_days.head({"date": 3}) |
| 3124 | <xarray.Dataset> Size: 72B |
| 3125 | Dimensions: (date: 3) |
| 3126 | Coordinates: |
| 3127 | * date (date) datetime64[us] 24B 2023-01-05 2023-01-04 2023-01-02 |