|
282 | 282 | "cell_type": "markdown",
|
283 | 283 | "metadata": {},
|
284 | 284 | "source": [
|
285 |
| - "## Reduce Function\n", |
| 285 | + "## The `map_rows` Function\n", |
286 | 286 | "\n",
|
287 |
| - "Finally, we'll end with the flexible `reduce` function. `reduce` functions similarly to pandas' `apply` but flattens (reduces) the inputs from nested layers into array inputs to the given apply function. For example, let's find the mean flux for each dataframe in \"nested\":" |
| 287 | + "Finally, we'll end with the flexible `map_rows` function. `map_rows` functions similarly to pandas' `apply` but applies row by row and flattens the inputs from nested layers into array inputs to the given apply function. For example, let's find the mean flux for each dataframe in \"nested\":" |
288 | 288 | ]
|
289 | 289 | },
|
290 | 290 | {
|
|
297 | 297 | "\n",
|
298 | 298 | "# use hierarchical column names to access the flux column\n",
|
299 | 299 | "# passed as an array to np.mean\n",
|
300 |
| - "nf.reduce(np.mean, \"lightcurve.brightness\")" |
| 300 | + "# row_container signals how to pass the data to the function, in this case as direct arguments\n", |
| 301 | + "nf.map_rows(np.mean, \"lightcurve.brightness\", row_container=\"args\")" |
301 | 302 | ]
|
302 | 303 | },
|
303 | 304 | {
|
|
313 | 314 | "metadata": {},
|
314 | 315 | "outputs": [],
|
315 | 316 | "source": [
|
316 |
| - "def show_inputs(*args):\n", |
317 |
| - " return args" |
| 317 | + "def show_inputs(row):\n", |
| 318 | + " return row" |
318 | 319 | ]
|
319 | 320 | },
|
320 | 321 | {
|
321 | 322 | "cell_type": "markdown",
|
322 | 323 | "metadata": {},
|
323 | 324 | "source": [
|
324 |
| - "Applying some inputs via reduce, we see how it sends inputs to a given function. The output frame `nf_inputs` consists of two columns containing the output of the “ra” column and the “lightcurve.time” column." |
| 325 | + "Applying some inputs via `map_rows`, we see how it sends inputs to a given function. The output frame `nf_inputs` consists of two columns containing the output of the “ra” column and the “lightcurve.time” column." |
325 | 326 | ]
|
326 | 327 | },
|
327 | 328 | {
|
|
330 | 331 | "metadata": {},
|
331 | 332 | "outputs": [],
|
332 | 333 | "source": [
|
333 |
| - "nf_inputs = nf.reduce(show_inputs, \"ra\", \"lightcurve.time\")\n", |
334 |
| - "nf_inputs" |
| 334 | + "# row_container=\"dict\" passes the data as a dictionary to the function\n", |
| 335 | + "nf_inputs = nf.map_rows(show_inputs, columns=[\"ra\", \"lightcurve.time\"], row_container=\"dict\")\n", |
| 336 | + "nf_inputs\n", |
| 337 | + "\n", |
| 338 | + "# map_rows returns a dataframe view of the dicts, but the two columns can be accessed with show_inputs as\n", |
| 339 | + "# row[\"ra\"] and row[\"lightcurve.time\"]" |
335 | 340 | ]
|
336 | 341 | },
|
337 | 342 | {
|
|
343 | 348 | "nf_inputs.loc[0]"
|
344 | 349 | ]
|
345 | 350 | },
|
| 351 | + { |
| 352 | + "cell_type": "code", |
| 353 | + "execution_count": null, |
| 354 | + "metadata": {}, |
| 355 | + "outputs": [], |
| 356 | + "source": [ |
| 357 | + "# row_container=\"args\" passes the data as arguments to the function\n", |
| 358 | + "\n", |
| 359 | + "\n", |
| 360 | + "def show_inputs(*args):\n", |
| 361 | + " return args\n", |
| 362 | + "\n", |
| 363 | + "\n", |
| 364 | + "nf_inputs = nf.map_rows(show_inputs, columns=[\"ra\", \"lightcurve.time\"], row_container=\"args\")\n", |
| 365 | + "nf_inputs" |
| 366 | + ] |
| 367 | + }, |
346 | 368 | {
|
347 | 369 | "cell_type": "markdown",
|
348 | 370 | "metadata": {},
|
|
0 commit comments