Repeat elements of a Series. Returns a new Series where each element of the current Series is repeated consecutively a given number of times. Parameters ---------- repeats : int or array of ints The number of repetitions for each element
(self, repeats: int | Sequence[int], axis: None = None)
| 1198 | # Unsorted |
| 1199 | |
| 1200 | def repeat(self, repeats: int | Sequence[int], axis: None = None) -> Series: |
| 1201 | """ |
| 1202 | Repeat elements of a Series. |
| 1203 | |
| 1204 | Returns a new Series where each element of the current Series |
| 1205 | is repeated consecutively a given number of times. |
| 1206 | |
| 1207 | Parameters |
| 1208 | ---------- |
| 1209 | repeats : int or array of ints |
| 1210 | The number of repetitions for each element. This should be a |
| 1211 | non-negative integer. Repeating 0 times will return an empty |
| 1212 | Series. |
| 1213 | axis : None |
| 1214 | Unused. Parameter needed for compatibility with DataFrame. |
| 1215 | |
| 1216 | Returns |
| 1217 | ------- |
| 1218 | Series |
| 1219 | Newly created Series with repeated elements. |
| 1220 | |
| 1221 | See Also |
| 1222 | -------- |
| 1223 | Index.repeat : Equivalent function for Index. |
| 1224 | numpy.repeat : Similar method for :class:`numpy.ndarray`. |
| 1225 | |
| 1226 | Examples |
| 1227 | -------- |
| 1228 | >>> s = pd.Series(["a", "b", "c"]) |
| 1229 | >>> s |
| 1230 | 0 a |
| 1231 | 1 b |
| 1232 | 2 c |
| 1233 | dtype: str |
| 1234 | >>> s.repeat(2) |
| 1235 | 0 a |
| 1236 | 0 a |
| 1237 | 1 b |
| 1238 | 1 b |
| 1239 | 2 c |
| 1240 | 2 c |
| 1241 | dtype: str |
| 1242 | >>> s.repeat([1, 2, 3]) |
| 1243 | 0 a |
| 1244 | 1 b |
| 1245 | 1 b |
| 1246 | 2 c |
| 1247 | 2 c |
| 1248 | 2 c |
| 1249 | dtype: str |
| 1250 | """ |
| 1251 | nv.validate_repeat((), {"axis": axis}) |
| 1252 | new_index = self.index.repeat(repeats) |
| 1253 | new_values = self._values.repeat(repeats) |
| 1254 | return self._constructor(new_values, index=new_index, copy=False).__finalize__( |
| 1255 | self, method="repeat" |
| 1256 | ) |
| 1257 |