<jupyter_start><jupyter_text>Tables and Units<jupyter_code>import numpy as np
import pandas as pd

from astropy import units as u
from astropy import constants as const
comet_table = pd.read_csv('./Data/Comets.csv')
comet_table<jupyter_output><empty_output><jupyter_text>`DataFrames` and units - A (not so good) example* Use `.to_numpy()` to pull data out of `DataFrame`* Then add the units<jupyter_code>semi_major = comet_table['Semi_Major_AU'].to_numpy() * u.AU
semi_major
semi_major.to(u.km)
comet_table['Semi_Major_km'] = semi_major.to(u.km)
comet_table<jupyter_output><empty_output><jupyter_text>Pull out column and assign units everytime you want to use them.* Even with dimensionless units (like `Eccentricity`).$$\large\mathrm{Perihelion\  Distance} = \mathrm{semi\ major\ axis} \times (1.0 - \mathrm{eccentricity})$$<jupyter_code>def find_perihelion(semi_major, eccentricity):
    result = semi_major * (1.0 - eccentricity)
    return result
my_semi_major = comet_table['Semi_Major_AU'].to_numpy() * u.AU

my_semi_major
my_ecc = comet_table['Eccentricity'].to_numpy() * u.dimensionless_unscaled

my_ecc
perihelion_AU = find_perihelion(my_semi_major, my_ecc)

perihelion_AU
comet_table['Perihelion_AU'] = perihelion_AU
comet_table<jupyter_output><empty_output><jupyter_text>Save `comet_table` to a file (`.csv`)<jupyter_code>comet_table.to_csv('./Data/Comet_DataFrame.csv', index=False)<jupyter_output><empty_output><jupyter_text>--- `DataFrames` and units * `DatFrames` and units do not play together well * Using a `DataFrame` and units requires you to:   * Pull out column and assign units everytime you want to use them.     * `comet_table['Semi_Major_AU'].to_numpy() * u.AU`   * The to save your results, without units, back to the table     * `comet_table['Perihelion_AU'] = perihelion_AU` --- Astropy `QTable`* A `QTable` = a table with units!* Does not have the huge number of `.methods` of a `DataFrame`* **Only** used by Astronomers* Can be easily converted to a `DataFrame`<jupyter_code>from astropy.table import QTable
comet_table = QTable.read('./Data/Comets.csv', format='ascii.csv')
comet_table
print(comet_table)<jupyter_output><empty_output><jupyter_text>Adding a unit to a column<jupyter_code>comet_table['Semi_Major_AU'].unit = u.AU
comet_table
comet_table['Semi_Major_AU']
comet_table['Semi_Major_AU'].to(u.km)
comet_table['Semi_Major_AU'].unit<jupyter_output><empty_output><jupyter_text>Functions and Tables$$\large\mathrm{Orbital\ Velocity} = \sqrt{GM_{\odot}\over d} $$<jupyter_code>def find_orbit_v(my_distance):
    result = np.sqrt(const.G * const.M_sun / my_distance)
    return result.decompose()
find_orbit_v(comet_table['Semi_Major_AU'])
comet_table['Oribtal_Velocity'] = find_orbit_v(comet_table['Semi_Major_AU'])
comet_table
comet_table['Oribtal_Velocity'].to(u.km / u.s)
comet_table['Oribtal_Velocity'].info.format = '.2f'
comet_table
comet_table.info()
for row in comet_table:
    output = f"The comet {row['Name']:9} has an orbital velocity of {row['Oribtal_Velocity']:.2f}"
    print(output)<jupyter_output><empty_output><jupyter_text>`QTable` manipulation and modification* Does not have the huge number of `.methods` of a `DataFrame`* Can do most 'obvious' stuff: slices, sorts, filtering, etc...* Documentation: [Astropy Table Modifications](https://het.as.utexas.edu/HET/Software/Astropy-1.0/table/modify_table.html)<jupyter_code>comet_table
comet_table['Name'][0]
comet_table[0:2]
comet_table[comet_table['Eccentricity'] < 0.8]
comet_table[comet_table['Eccentricity'] < 0.8]['Name'][-1]
comet_table.rename_column('Name', 'Comet Name')
comet_table
comet_table.sort('Oribtal_Velocity')
comet_table
comet_table['Comet Name'][0]
comet_table.sort('Oribtal_Velocity', reverse=True)

comet_table<jupyter_output><empty_output><jupyter_text>Can save `QTables` with all the units info intact (`.ecsv`).<jupyter_code>comet_table.write('./Data/Comet_QTable.ecsv', format='ascii.ecsv', overwrite=True)
my_new_table = QTable.read('./Data/Comet_QTable.ecsv', format='ascii.ecsv')
my_new_table<jupyter_output><empty_output><jupyter_text>--- Can convert astropy `QTable` to pandas `DataFrame` - Loose all units info :(<jupyter_code>comet_table_pandas = QTable(comet_table).to_pandas()
comet_table_pandas<jupyter_output><empty_output><jupyter_text>Can convert pandas `DataFrame` to astropy `QTable` - Will need to put the units back on :(<jupyter_code>comet_table_astropy = QTable.from_pandas(comet_table_pandas)
comet_table_astropy<jupyter_output><empty_output><jupyter_text>Long Tables<jupyter_code>long_table= QTable.read('./Data/Comets_100.csv', format='ascii.csv')
long_table
long_table.show_in_notebook()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12995076000856348, 'fraction_numerical': 0.00449582530507386, 'mean_word_length': 7.556776556776557, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 49, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '193128', 'license': 'MIT', 'n_tokens_mistral': 1812, 'n_tokens_neox': 1750, 'n_words': 331, 'path': 'Python_Units_Tables.ipynb', 'repo_name': 'UWashington-Astro300/Astro300-A21', 'size': '4671'}