<jupyter_start><jupyter_text>xarray SandboxFor playing around during development.<jupyter_code>import xarray as xr
import numpy as np
import pysd.utils as utils
import textwrap<jupyter_output><empty_output><jupyter_text>creating dataarrays with data at instantiation<jupyter_code>coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
a = xr.DataArray(data=[[.1, .2],[.3,.4],[.5,.6]], coords=coords, dims=dims)
a
a = xr.DataArray(data=[3, 4, 5], coords={'Dim1': ['A', 'B', 'C']})
print a, '\n'
b = xr.DataArray(data=a, coords={'Dim1': ['A', 'B', 'C']})
print b
a = xr.DataArray(data=[3, 4, 5], coords={'Dim1': ['A', 'B', 'C']})
print a, '\n'
b = xr.DataArray(data=a, coords={'Dim1': ['D', 'E', 'F']})
print b
a = xr.DataArray(data=[3, 4, 5], coords={'Dim1': ['A', 'B', 'C']})
print a
b = xr.DataArray(data=a, coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}, dims=['Dim1', 'Dim2'])
print b

a = xr.DataArray(data=3, coords={'Dim1': ['A', 'B', 'C']})
subs = ['One Dimensional Subscript', 'Second Dimension Subscript', 'Depth 1']
subscript_dict = {'Second Dimension Subscript': ['Column 1', 'Column 2'], 
                  'Third Dimension Subscript': ['Depth 1', 'Depth 2'], 
                  'One Dimensional Subscript': ['Entry 1', 'Entry 2', 'Entry 3']}

coords = utils.make_coord_dict(subs, subscript_dict, terse=False)
dims = [utils.find_subscript_name(subscript_dict, sub) for sub in subs]
shape = [len(coords[dim]) for dim in dims]

text = '1,2; 3,4; 5,6'
text = text.strip(';').replace(' ', '').replace(';',',')

data = np.array([float(s) for s in text.split(',')]).reshape(shape)
datastr = np.array2string(data, separator=',').replace('\n','').replace(' ','')

datastr
a = xr.DataArray(data=data, coords=coords, dims=dims)
a

string = textwrap.dedent("""\
        xr.DataArray(data=%(datastr)s,
                     coords=%(coords)s,
                     dims=%(dims)s )""" % {
        'datastr': datastr,
        'coords': coords,
        'dims': repr(dims)
    })

eval(string)
# modify values directly without index slicing
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
shape = [len(coords[key]) for key in dims]
val = [3, 2, 1]
a = xr.DataArray(data=np.tile(val, shape), coords=coords, dims=dims)

a<jupyter_output><empty_output><jupyter_text>creating empty dataarrays<jupyter_code># using np.zeros
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
shape = [len(coords[key]) for key in dims]
a = xr.DataArray(data=np.zeros(shape)*np.NaN, coords=coords, dims=dims)
a.loc[coords]
# using np.tile
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
shape = [len(coords[key]) for key in dims]
a = xr.DataArray(data=np.tile(np.NaN, shape), coords=coords, dims=dims)
a.loc[coords]<jupyter_output><empty_output><jupyter_text>assigning all values to array<jupyter_code># assign all values to a scalar
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
shape = [len(coords[key]) for key in dims]
a = xr.DataArray(data=np.tile(np.NaN, shape), coords=coords, dims=dims)

a.loc[coords] = 3
a.loc[coords]
# fill all values
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
shape = [len(coords[key]) for key in dims]
a = xr.DataArray(data=np.tile(np.NaN, shape), coords=coords, dims=dims)

a.loc[coords] = [1,2],[3,4],[5,6]
a.loc[coords]
# modify values directly without index slicing
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
shape = [len(coords[key]) for key in dims]
a = xr.DataArray(data=np.tile(np.NaN, shape), coords=coords, dims=dims)

values = [10,20],[30,40],[50,60]
a.values = np.array(values)
a.loc[coords]
# modify values directly without index slicing
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
shape = [len(coords[key]) for key in dims]
a = xr.DataArray(data=np.tile(np.NaN, shape), coords=coords, dims=dims)

a.loc[]
a.values = np.array(values)
a.loc[coords]
# select a slice
a.loc[{'Dim2':['D']}]<jupyter_output><empty_output><jupyter_text>Assign to a slice<jupyter_code># assign to a slice (the orientation matters!)
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
shape = [len(coords[key]) for key in dims]
a = xr.DataArray(data=np.tile(np.NaN, shape), coords=coords, dims=dims)

a.loc[{'Dim2':['D']}] = 1, 2, 3
a.loc[coords]
#assign to a slice with properly aligned array
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
shape = [len(coords[key]) for key in dims]
a = xr.DataArray(data=np.tile(np.NaN, shape), coords=coords, dims=dims)

a.loc[{'Dim2':['D']}] = [1], [2], [3]
a.loc[coords]

# assign to opposite dimension
ret = xr.DataArray(data=np.ones([3, 2])*np.NaN,
             coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']},
             dims=['Dim1', 'Dim2'] )
ret.loc[{'Dim1': ['A']}] = 50, 51
ret.loc[{'Dim1': ['B']}] = 21, 22
ret.loc[{'Dim1': ['C']}] = 3, 4
ret<jupyter_output><empty_output><jupyter_text>update a slice based on an existing dataarray<jupyter_code># add a new slice
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
shape = [len(coords[key]) for key in dims]
a = xr.DataArray(data=np.tile(np.NaN, shape), coords=coords, dims=dims)

new_coords={'Dim1': ['A', 'B', 'C'], 'Dim2': ['D']}
new_slice = xr.DataArray(data=[[1.1, 2.2, 3.3]], coords=new_coords)

a.update(new_slice)
a
a.loc[new_coords] = new_slice
a.loc[new_coords].values = new_slice.loc[new_coords].values
# multiplication works, why not assignment?
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
a = xr.DataArray(data=[[.1, .2],[.3,.4],[.5,.6]], coords=coords, dims=dims)

new_coords={'Dim1': ['A', 'B', 'C'], 'Dim2': ['D']}
new_slice = xr.DataArray(data=[[1.1, 2.2, 3.3]], coords=new_coords)

a.loc[new_coords] * new_slice.loc[new_coords]
a * new_slice
a + new_slice
#a2, new_slice2 = xr.align(a,new_slice)
a.loc[new_coords]
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
shape = [len(coords[key]) for key in dims]
a = xr.DataArray(data=np.tile(np.NaN, shape), coords=coords, dims=dims)
print 'Original Array:\n-----------------\n', a, '\n'

new_coords={'Dim1': ['A', 'B', 'C'], 'Dim2': ['D']}
new_slice = xr.DataArray(data=[[1.1, 2.2, 3.3]], coords=new_coords)
print 'New Slice:\n-----------------\n', new_slice, '\n'

ds = xr.Dataset({'a':a})
ds.update(xr.Dataset({'a':new_slice}))
print 'Updated Array:\n-----------------\n', b['a'], '\n'<jupyter_output>Original Array:
-----------------
<xarray.DataArray (Dim1: 3, Dim2: 2)>
array([[ nan,  nan],
       [ nan,  nan],
       [ nan,  nan]])
Coordinates:
  * Dim2     (Dim2) |S1 'D' 'E'
  * Dim1     (Dim1) |S1 'A' 'B' 'C' 

New Slice:
-----------------
<xarray.DataArray (Dim2: 1, Dim1: 3)>
array([[ 1.1,  2.2,  3.3]])
Coordinates:
  * Dim2     (Dim2) |S1 'D'
  * Dim1     (Dim1) |S1 'A' 'B' 'C' 

Updated Array:
-----------------
<xarray.DataArray 'a' (Dim2: 2, Dim1: 3)>
array([[ 1.1,  2.2,  3.3],
       [ nan,  nan,  nan]])
Coordinates:
  * Dim2     (Dim2) object 'D' 'E'
  * Dim1     (Dim1) |S1 'A' 'B' 'C'<jupyter_text>Using array concatenation<jupyter_code>m = xr.DataArray(data=1, 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D']})
m
m = xr.DataArray(data=[1.1, 1.2, 1.3], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D']})
n = xr.DataArray(data=[2.1, 2.2, 2.3], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E']})
m = xr.DataArray(data=[[1.1, 1.2, 1.3]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D']})
n = xr.DataArray(data=[[2.1, 2.2, 2.3]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E']})

a = xr.concat([m, n], dim='Dim2')
a
m = xr.DataArray(data=[[[1.1, 1.2, 1.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['F']})
n = xr.DataArray(data=[[[2.1, 2.2, 2.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['F']})
o = xr.DataArray(data=[[[3.1, 3.2, 3.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['G']})
p = xr.DataArray(data=[[[4.1, 4.2, 4.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['G']})

a = xr.concat([m,n,o,p], dim=['Dim2', 'Dim3'])
m = xr.DataArray(data=[[[1.1, 1.2, 1.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['F']})
n = xr.DataArray(data=[[[2.1, 2.2, 2.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['F']})
o = xr.DataArray(data=[[[3.1, 3.2, 3.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['G']})
p = xr.DataArray(data=[[[4.1, 4.2, 4.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['G']})
a = xr.align(m,n,o,p, join='outer')
xr.concat(a, 'Dim1')
m = xr.DataArray(data=[[[1.1, 1.2, 1.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['F']})
n = xr.DataArray(data=[[[2.1, 2.2, 2.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['F']})
o = xr.DataArray(data=[[[3.1, 3.2, 3.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['G']})
p = xr.DataArray(data=[[[4.1, 4.2, 4.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['G']})
a = xr.align(m,n,o,p, join='outer')
xr.concat(a, dim=['Dim2', 'Dim3'])
m = xr.DataArray(data=[[[1.1, 1.2, 1.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['F']})
n = xr.DataArray(data=[[[2.1, 2.2, 2.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['F']})
o = xr.DataArray(data=[[[3.1, 3.2, 3.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['G']})
p = xr.DataArray(data=[[[4.1, 4.2, 4.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['G']})

def xrmerge(das, accept_new=True):
    da = das[0]
    for new_da in das[1:]:
        # Expand both to have same dimensions, padding with NaN
        da, new_da = xr.align(da, new_da, join='outer')
        # Fill NaNs one way or the other re. accept_new
        da = new_da.fillna(da) if accept_new else da.fillna(new_da)
    return da

a = xrmerge([m,n,o,p])
a
a.loc[{'Dim1':'A'}]
a.loc[{'Dim2':'D'}]
a.loc[{'Dim3':'F'}]
data =[[[ 1.1,  1.2,  1.3],
        [ 3.1,  3.2,  3.3]],

       [[ 2.1,  2.2,  2.3],
        [ 4.1,  4.2,  4.3]]]

b = xr.DataArray(data=data, 
             coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D', 'E'], 'Dim3':['F', 'G']})
b.values
for i in b.coords.iteritems():
    print i[0], i[1].values

{key:list(value.values) for key, value in b.coords.iteritems()}
for i in b.indexes.iteritems():
    print i[0], i[1].values
m = xr.DataArray(data=[[[1.1, 1.2, 1.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['F']})
n = xr.DataArray(data=[[[2.1, 2.2, 2.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['F']})
o = xr.DataArray(data=[[[3.1, 3.2, 3.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['G']})
p = xr.DataArray(data=[[[4.1, 4.2, 4.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['G']})
ds = xr.Dataset({'m':m,'n':n,'o':o,'p':p})
ds.to_array()
m = xr.DataArray(data=[[[1.1, 1.2, 1.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['F']})
n = xr.DataArray(data=[[[2.1, 2.2, 2.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['F']})
o = xr.DataArray(data=[[[3.1, 3.2, 3.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['G']})
p = xr.DataArray(data=[[[4.1, 4.2, 4.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['G']})
l = [n,o,p]
for j in l:
    xr.concat([m, j])
    break
base = xr.DataArray(data=[[[1.1, 1.2, 1.3]]], 
                    coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['F']})
n = xr.DataArray(data=[[[2.1, 2.2, 2.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['F']})
o = xr.DataArray(data=[[[3.1, 3.2, 3.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['G']})
p = xr.DataArray(data=[[[4.1, 4.2, 4.3]]], 
                 coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['G']})<jupyter_output><empty_output><jupyter_text>in pandas<jupyter_code>import pandas as pd
df = pd.DataFrame(index=range(5), columns=['a','b','c','d'])
df2 = pd.DataFrame(index=range(3), columns=['a'], data=range(3))
df.update(df2)
df
df.loc[2:4, 'b'] = 5
df<jupyter_output><empty_output><jupyter_text>df.loc[2:4, 'c'] = [5,6,7]df  Trying all the types of functions<jupyter_code>upstream_funcs = [
    lambda: 5
    lambda: xr.DataArray(data=5, coords={'Dim1': ['A', 'B', 'C']})
    lambda: xr.DataArray(data=5, coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D']})
    lambda: xr.DataArray(data=5, coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D', 'E']})
]

a.coords.values()
{key:list(val.values) for key, val in a.coords.iteritems()}<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.29720439963336387, 'fraction_numerical': 0.04330889092575619, 'mean_word_length': 3.743840579710145, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 0, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '311513', 'license': 'MIT', 'n_tokens_mistral': 5970, 'n_tokens_neox': 5916, 'n_words': 1214, 'path': 'Xarray value assignment.ipynb', 'repo_name': 'arnislektauers/pysd', 'size': '13092'}