Skip to content

Do not modify the input astropy table when converting to pandas dataframe #268

@jasongrout

Description

@jasongrout

How could the content be improved?

In #74, the function that makes a pandas dataframe from an astropy table was updated to modify the input astropy table (adding some columns). In general, it's probably not a good idea to modify an input to a function unless the function is specifically designed to modify its input in place. However, I understand the change was made to resolve some errors in a pandas version update.

It seems that using .value when adding a skycoord sequence to pandas works now, e.g., df['phi1'] = skycoord_gd1.phi1.value:

def make_dataframe(table):
    """Transform and astropy table with coords in ICRS, convert to pandas dataframe with GD-1 coordinates."""
    skycoord = SkyCoord(
        ra=table['ra'],
        dec=table['dec'],
        pm_ra_cosdec=table['pmra'],
        pm_dec=table['pmdec'],
        distance=8*u.kpc,
        radial_velocity=0*u.km/u.s)

    # Transform to GD-1 frame
    gd1_frame = GD1Koposov10()
    transformed = skycoord.transform_to(gd1_frame)
    skycoord_gd1 = reflex_correct(transformed)

    df = table.to_pandas()
    df['phi1'] = skycoord_gd1.phi1.value
    df['phi2'] = skycoord_gd1.phi2.value
    df['pm_phi1'] = skycoord_gd1.pm_phi1_cosphi2.value
    df['pm_phi2'] = skycoord_gd1.pm_phi2.value

    return df

Which part of the content does your suggestion apply to?

https://datacarpentry.github.io/astronomy-python/03-transform.html#pandas-dataframe

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions