Assigning coordinates in existing xarray dataset and changing format for epoch

34 views Asked by At

Currently I have a pandas dataframe that looks like this (It is originally from a large csv file, I have included a small snippet)

ds = pd.DataFrame([('2020-312T00:00:00.746', 2.466000e-15, 2.330500e-15, 2.949800e-15, 7.497400e-15, 3.682900e-15, 6.375300e-15),
                   ('2020-312T00:00:01.746', 1.406300e-14, 1.319700e-14, 6.588400e-15, 5.245300e-15, 4.462600e-15, 6.375300e-15),
                   ('2020-312T00:00:02.746', 9.389400e-15, 7.542200e-15, 5.433000e-15, 2.355100e-15, 7.388700e-15, 3.852900e-15),
                   ('2020-312T00:00:03.746', 5.252700e-15, 4.338100e-15, 6.588400e-15, 5.245300e-15, 2.934300e-15, 6.375300e-15),
                   ('2020-312T00:00:04.746', 5.252700e-15, 9.345600e-15, 3.340000e-15, 5.245300e-15, 8.766700e-15, 4.640600e-15)],
                   columns=['Epoch', ' Freq_1.12E+04', 'Freq_1.25E+04', 'Freq_1.41E+04', 'Freq_1.58E+04', 'Freq_1.77E+04', 
                           'Freq_1.98E+04'])
ds

I converted this dataframe to xarray using new_ds = ds.to_xarray() and it prints out a dataset that looks like -- enter image description here

I want to change this dataset by setting the Epoch as index, and then change the coordinates to Epoch and the frequencies. And change the dimensions to epoch and the frequency (x and y axis resp). How can I go about this?

1

There are 1 answers

0
Serge de Gosson de Varennes On

You simply need to set the epoch as index

import pandas as pd
import xarray as xr

ds = pd.DataFrame([
    ('2020-312T00:00:00.746', 2.466000e-15, 2.330500e-15, 2.949800e-15, 7.497400e-15, 3.682900e-15, 6.375300e-15),
    ('2020-312T00:00:01.746', 1.406300e-14, 1.319700e-14, 6.588400e-15, 5.245300e-15, 4.462600e-15, 6.375300e-15),
    ('2020-312T00:00:02.746', 9.389400e-15, 7.542200e-15, 5.433000e-15, 2.355100e-15, 7.388700e-15, 3.852900e-15),
    ('2020-312T00:00:03.746', 5.252700e-15, 4.338100e-15, 6.588400e-15, 5.245300e-15, 2.934300e-15, 6.375300e-15),
    ('2020-312T00:00:04.746', 5.252700e-15, 9.345600e-15, 3.340000e-15, 5.245300e-15, 8.766700e-15, 4.640600e-15)
], columns=['Epoch', 'Freq_1.12E+04', 'Freq_1.25E+04', 'Freq_1.41E+04', 'Freq_1.58E+04', 'Freq_1.77E+04', 'Freq_1.98E+04'])

ds = ds.set_index('Epoch')

xds = ds.to_xarray()

xds

which gives you

enter image description here