Skip to content

Commit

Permalink
Merge pull request #27 from pysat/remove_panel
Browse files Browse the repository at this point in the history
Remove panel
  • Loading branch information
rstoneback committed Jan 27, 2022
2 parents fa836a9 + 694f87b commit bbb8271
Show file tree
Hide file tree
Showing 10 changed files with 319 additions and 153 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [0.2.0] - 2022-XX-XX
- Removed deprecated `pandas.Panel` from functions.
- Renamed `computational_form` to `to_xarray_dataset` and refocused.
- Removed old __future__ imports.

## [0.1.3] - 2021-06-18
- Updates style to match pysat 3.0.0 release candidate
- Improves discussion of rationale for version caps on readme page
Expand Down
2 changes: 1 addition & 1 deletion pysatSeasons/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from pysatSeasons import occur_prob # noqa: F401
from pysatSeasons import avg # noqa: F401
from pysatSeasons import plot # noqa: F401
from pysatSeasons._core import computational_form # noqa: F401
from pysatSeasons._core import to_xarray_dataset # noqa: F401

# set version
here = os.path.abspath(os.path.dirname(__file__))
Expand Down
92 changes: 69 additions & 23 deletions pysatSeasons/_core.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,84 @@
import pandas as pds
import xarray as xr


def computational_form(data):
"""
Repackages numbers, Series, or DataFrames
def to_xarray_dataset(data):
"""Repackage input data into `xarray.Dataset`.
Regardless of input format, mathematical operations may be performed on the
output via the same pandas mechanisms.
This method may be particularly useful in analysis methods that aim to be
instrument independent. pysat.Instrument objects can package data in a
variety of ways within a DataFrame, depending upon the scientific data
source. Thus, a variety of data types will be encountered by instrument
independent methods and computational_form method may reduce the effort
required to support more generalized processing.
output via the same xarray mechanisms. This method may be particularly
useful in analysis methods that aim to be instrument independent.
pysat.Instrument objects can package data in a variety of ways within a
DataFrame, depending upon the scientific data source. Thus, a variety of
data types will be encountered by instrument independent methods and
`to_xarray_dataset` method may reduce the effort required to support more
generalized processing.
Parameters
----------
data : pandas.Series
Series of numbers, Series, DataFrames
data : pds.Series, pds.DataFrame, xr.DataArray, xr.DataSet, or list-like
of the same or numbers.
List-like of numbers, Series, DataFrames, or Datasets to be combined
into a single Dataset.
Returns
-------
pandas.Series, DataFrame, or Panel
repacked data, aligned by indices, ready for calculation
output : xr.Dataset
Repacked data, aligned by indices. If data is a list of multidimensional
objects then output will have a corresponding new dimension
'pysat_binning' to reflect that organization. If data is a list of
numbers, then output will have a single associated variable, `data`.
Otherwise, variable names are retained from the input data.
"""

if isinstance(data.iloc[0], pds.DataFrame):
dslice = pds.Panel.from_dict(dict([(i, data.iloc[i])
for i in range(len(data))]))
elif isinstance(data.iloc[0], pds.Series):
dslice = pds.DataFrame(data.tolist())
dslice.index = data.index
if isinstance(data, pds.DataFrame):
output = data.to_xarray()
elif isinstance(data, pds.Series):
output = xr.Dataset()
output[data.name] = data.to_xarray()
elif isinstance(data, xr.Dataset):
output = data
elif isinstance(data, xr.DataArray):
output = xr.Dataset()
output[data.name] = data
elif isinstance(data[0], xr.Dataset):
# Combine list-like of Datasets
vars = data[0].data_vars.keys()
output = xr.Dataset()

# Combine all info for each variable into a single data
# array.
for var in vars:
output[var] = xr.concat([item[var] for item in data],
'pysat_binning')
elif isinstance(data[0], xr.DataArray):
# Combine list-like of DataArrays
vars = [data[0].name]
output = xr.Dataset()

for var in vars:
output[var] = xr.concat(data, 'pysat_binning')
elif isinstance(data[0], pds.DataFrame):
# Convert list-like of DataFrames
info = [xr.Dataset.from_dataframe(item) for item in data]

vars = info[0].data_vars.keys()
output = xr.Dataset()

for var in vars:
output[var] = xr.concat([item[var] for item in info],
'pysat_binning')
elif isinstance(data[0], pds.Series):
# Convert list-like of pds.Series
vars = [data[0].name]
output = xr.Dataset()

for var in vars:
output[var] = xr.concat([xr.DataArray.from_series(item)
for item in data], 'pysat_binning')
else:
dslice = data
return dslice
output = xr.Dataset()
output['data'] = data

return output

0 comments on commit bbb8271

Please sign in to comment.