update replace and loc functions of DataFrame

This commit is contained in:
wyq 2023-06-14 13:10:55 +08:00
parent 223b8fd231
commit 37fb27ed17
9 changed files with 298 additions and 50 deletions

View File

@ -1396,11 +1396,6 @@ public class DataFrame implements Iterable {
* @throws org.meteoinfo.ndarray.InvalidRangeException * @throws org.meteoinfo.ndarray.InvalidRangeException
*/ */
public void setValues(int row, Range colRange, Number value) throws InvalidRangeException { public void setValues(int row, Range colRange, Number value) throws InvalidRangeException {
ColumnIndex cols = new ColumnIndex();
for (int i = colRange.first(); i <= colRange.last(); i += colRange.stride()) {
cols.add((Column) this.columns.get(i).clone());
}
if (this.array2D) { if (this.array2D) {
List ranges = new ArrayList<>(); List ranges = new ArrayList<>();
ranges.add(new Range(row, row, 1)); ranges.add(new Range(row, row, 1));
@ -1413,6 +1408,186 @@ public class DataFrame implements Iterable {
} }
} }
/**
* Set values by row and column ranges
*
* @param rowRange Row range
* @param colRange Column range
* @param value The value
* @throws org.meteoinfo.ndarray.InvalidRangeException
*/
public void setValues(Range rowRange, Range colRange, Number value) throws InvalidRangeException {
if (this.array2D) {
List ranges = new ArrayList<>();
ranges.add(rowRange);
ranges.add(colRange);
ArrayMath.setSection((Array) this.data, ranges, value);
} else {
for (int j = colRange.first(); j <= colRange.last(); j += colRange.stride()) {
Array array = ((List<Array>) this.data).get(j);
for (int i = rowRange.first(); i <= rowRange.last(); i += rowRange.stride()) {
array.setObject(i, value);
}
}
}
}
/**
* Set values by row and column ranges
*
* @param rowRange Row range
* @param colRange Column range
* @param value The value
* @throws org.meteoinfo.ndarray.InvalidRangeException
*/
public void setValues(List<Integer> rowRange, Range colRange, Number value) throws InvalidRangeException {
if (this.array2D) {
List ranges = new ArrayList<>();
ranges.add(rowRange);
ranges.add(colRange);
ArrayMath.setSection_Mix((Array) this.data, ranges, value);
} else {
for (int j = colRange.first(); j <= colRange.last(); j += colRange.stride()) {
Array array = ((List<Array>) this.data).get(j);
for (int i : rowRange) {
array.setObject(i, value);
}
}
}
}
/**
* Set values by row and column ranges
*
* @param rowRange Row range
* @param colRange Column range
* @param value The value
* @throws org.meteoinfo.ndarray.InvalidRangeException
*/
public void setValues(Range rowRange, List<Integer> colRange, Number value) throws InvalidRangeException {
if (this.array2D) {
List ranges = new ArrayList<>();
ranges.add(rowRange);
ranges.add(colRange);
ArrayMath.setSection_Mix((Array) this.data, ranges, value);
} else {
for (int j : colRange) {
Array array = ((List<Array>) this.data).get(j);
for (int i = rowRange.first(); i <= rowRange.last(); i += rowRange.stride()) {
array.setObject(i, value);
}
}
}
}
/**
* Set values by row and column ranges
*
* @param rowRange Row range
* @param colRange Column range
* @param value The value
* @throws org.meteoinfo.ndarray.InvalidRangeException
*/
public void setValues(List<Integer> rowRange, List<Integer> colRange, Number value) throws InvalidRangeException {
if (this.array2D) {
List ranges = new ArrayList<>();
ranges.add(rowRange);
ranges.add(colRange);
ArrayMath.setSection_List((Array) this.data, ranges, value);
} else {
for (int j : colRange) {
Array array = ((List<Array>) this.data).get(j);
for (int i : rowRange) {
array.setObject(i, value);
}
}
}
}
/**
* Set values by row and column ranges
*
* @param rowRange Row range
* @param colRange Column range
* @param value The value
* @throws org.meteoinfo.ndarray.InvalidRangeException
*/
public void setValues(List<Integer> rowRange, Range colRange, Array value) throws InvalidRangeException {
value = value.copyIfView();
if (this.array2D) {
List ranges = new ArrayList<>();
ranges.add(rowRange);
ranges.add(colRange);
ArrayMath.setSection_Mix((Array) this.data, ranges, value);
} else {
int idx = 0;
for (int j = colRange.first(); j <= colRange.last(); j += colRange.stride()) {
Array array = ((List<Array>) this.data).get(j);
for (int i : rowRange) {
array.setObject(i, value.getObject(idx));
idx += 1;
}
}
}
}
/**
* Set values by row and column ranges
*
* @param rowRange Row range
* @param colRange Column range
* @param value The value
* @throws org.meteoinfo.ndarray.InvalidRangeException
*/
public void setValues(Range rowRange, List<Integer> colRange, Array value) throws InvalidRangeException {
value = value.copyIfView();
if (this.array2D) {
List ranges = new ArrayList<>();
ranges.add(rowRange);
ranges.add(colRange);
ArrayMath.setSection_Mix((Array) this.data, ranges, value);
} else {
int idx = 0;
for (int j : colRange) {
Array array = ((List<Array>) this.data).get(j);
for (int i = rowRange.first(); i <= rowRange.last(); i += rowRange.stride()) {
array.setObject(i, value.getObject(idx));
idx += 1;
}
}
}
}
/**
* Set values by row and column ranges
*
* @param rowRange Row range
* @param colRange Column range
* @param value The value
* @throws org.meteoinfo.ndarray.InvalidRangeException
*/
public void setValues(List<Integer> rowRange, List<Integer> colRange, Array value) throws InvalidRangeException {
value = value.copyIfView();
if (this.array2D) {
List ranges = new ArrayList<>();
ranges.add(rowRange);
ranges.add(colRange);
ArrayMath.setSection_List((Array) this.data, ranges, value);
} else {
int idx = 0;
for (int j : colRange) {
Array array = ((List<Array>) this.data).get(j);
for (int i : rowRange) {
array.setObject(i, value.getObject(idx));
idx += 1;
}
}
}
}
/** /**
* Set values by row and column ranges * Set values by row and column ranges
* *
@ -1422,6 +1597,8 @@ public class DataFrame implements Iterable {
* @throws org.meteoinfo.ndarray.InvalidRangeException * @throws org.meteoinfo.ndarray.InvalidRangeException
*/ */
public void setValues(int row, Range colRange, Array value) throws InvalidRangeException { public void setValues(int row, Range colRange, Array value) throws InvalidRangeException {
value = value.copyIfView();
ColumnIndex cols = new ColumnIndex(); ColumnIndex cols = new ColumnIndex();
for (int i = colRange.first(); i <= colRange.last(); i += colRange.stride()) { for (int i = colRange.first(); i <= colRange.last(); i += colRange.stride()) {
cols.add((Column) this.columns.get(i).clone()); cols.add((Column) this.columns.get(i).clone());
@ -1441,6 +1618,39 @@ public class DataFrame implements Iterable {
} }
} }
/**
* Set values by row and column ranges
*
* @param rowRange Row range
* @param colRange Column range
* @param value The value array
* @throws org.meteoinfo.ndarray.InvalidRangeException
*/
public void setValues(Range rowRange, Range colRange, Array value) throws InvalidRangeException {
value = value.copyIfView();
ColumnIndex cols = new ColumnIndex();
for (int i = colRange.first(); i <= colRange.last(); i += colRange.stride()) {
cols.add((Column) this.columns.get(i).clone());
}
if (this.array2D) {
List ranges = new ArrayList<>();
ranges.add(rowRange);
ranges.add(colRange);
ArrayMath.setSection((Array) this.data, ranges, value);
} else {
int idx = 0;
for (int j = colRange.first(); j <= colRange.last(); j += colRange.stride()) {
Array array = ((List<Array>) this.data).get(j);
for (int i = rowRange.first(); i <= rowRange.last(); i += rowRange.stride()) {
array.setObject(i, value.getObject(idx));
idx += 1;
}
}
}
}
/** /**
* Select by row and column ranges * Select by row and column ranges
* *

View File

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<MeteoInfo File="milconfig.xml" Type="configurefile"> <MeteoInfo File="milconfig.xml" Type="configurefile">
<Path OpenPath="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\surf"> <Path OpenPath="D:\Working\MIScript\Jython\mis\dataframe">
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\io\dataconvert"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\io"/> <RecentFolder Folder="D:\Working\MIScript\Jython\mis\io"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\io\burf"/> <RecentFolder Folder="D:\Working\MIScript\Jython\mis\io\burf"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\LaSW"/> <RecentFolder Folder="D:\Working\MIScript\Jython\mis\LaSW"/>
@ -10,27 +9,28 @@
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\slice"/> <RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\slice"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\contour"/> <RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\contour"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\funny"/> <RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\funny"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\array"/> <RecentFolder Folder="D:\Working\MIScript\Jython\mis\array"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types"/> <RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\3d"/> <RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\3d"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl"/> <RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\surf"/> <RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\surf"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\dataframe"/>
</Path> </Path>
<File> <File>
<OpenedFiles> <OpenedFiles>
<OpenedFile File="D:\MyProgram\java\MeteoInfoDev\toolbox\meteoview3d\_reload.py"/> <OpenedFile File="D:\MyProgram\java\MeteoInfoDev\toolbox\meteoview3d\_reload.py"/>
<OpenedFile File="D:\MyProgram\java\MeteoInfoDev\toolbox\meteoview3d\mainGUI.py"/> <OpenedFile File="D:\MyProgram\java\MeteoInfoDev\toolbox\meteoview3d\mainGUI.py"/>
<OpenedFile File="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\slice\slice_2d.py"/> <OpenedFile File="D:\Working\MIScript\Jython\mis\dataframe\loc_2.py"/>
<OpenedFile File="D:\Working\MIScript\Jython\mis\plot_types\funny\butterfly.py"/> <OpenedFile File="D:\Working\MIScript\Jython\mis\dataframe\replace_3.py"/>
<OpenedFile File="D:\Working\MIScript\Jython\mis\array\mgrid_1.py"/> <OpenedFile File="D:\Working\MIScript\Jython\mis\dataframe\loc_1.py"/>
</OpenedFiles> </OpenedFiles>
<RecentFiles> <RecentFiles>
<RecentFile File="D:\MyProgram\java\MeteoInfoDev\toolbox\meteoview3d\_reload.py"/> <RecentFile File="D:\MyProgram\java\MeteoInfoDev\toolbox\meteoview3d\_reload.py"/>
<RecentFile File="D:\MyProgram\java\MeteoInfoDev\toolbox\meteoview3d\mainGUI.py"/> <RecentFile File="D:\MyProgram\java\MeteoInfoDev\toolbox\meteoview3d\mainGUI.py"/>
<RecentFile File="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\slice\slice_2d.py"/> <RecentFile File="D:\Working\MIScript\Jython\mis\dataframe\loc_2.py"/>
<RecentFile File="D:\Working\MIScript\Jython\mis\plot_types\funny\butterfly.py"/> <RecentFile File="D:\Working\MIScript\Jython\mis\dataframe\replace_3.py"/>
<RecentFile File="D:\Working\MIScript\Jython\mis\array\mgrid_1.py"/> <RecentFile File="D:\Working\MIScript\Jython\mis\dataframe\loc_1.py"/>
</RecentFiles> </RecentFiles>
</File> </File>
<Font> <Font>
@ -38,5 +38,5 @@
</Font> </Font>
<LookFeel DockWindowDecorated="true" LafDecorated="true" Name="FlatDarkLaf"/> <LookFeel DockWindowDecorated="true" LafDecorated="true" Name="FlatDarkLaf"/>
<Figure DoubleBuffering="true"/> <Figure DoubleBuffering="true"/>
<Startup MainFormLocation="-7,-7" MainFormSize="1293,685"/> <Startup MainFormLocation="-7,0" MainFormSize="1366,778"/>
</MeteoInfo> </MeteoInfo>

View File

@ -478,15 +478,74 @@ class DataFrame(object):
def _setitem_loc(self, key, value): def _setitem_loc(self, key, value):
if isinstance(value, datetime.datetime): if isinstance(value, datetime.datetime):
value = [miutil.jdatetime(value)] value = miutil.jdatetime(value)
if isinstance(value, (list, tuple)): if isinstance(value, (list, tuple)):
if isinstance(value[0], datetime.datetime): value = np.array(value)
value = miutil.jdatetime(value)
#value = np.array(value)
if isinstance(value, np.NDArray): if isinstance(value, np.NDArray):
value = value._array value = value._array
self._dataframe.setRow(key, value) if not isinstance(key, tuple):
key = (key, None)
k = key[0]
rkeys = key[0]
if isinstance(k, slice):
sidx = 0 if k.start is None else self._index.index(k.start)
if sidx < 0:
raise KeyError(key)
eidx = self.shape[0] - 1 if k.stop is None else self._index.index(k.stop)
if eidx < 0:
raise KeyError(key)
step = 1 if k.step is None else k.step
rowkey = Range(sidx, eidx, step)
else:
rloc = self._index.get_loc(k, outkeys=True)
if isinstance(rloc, tuple):
rowkey = rloc[0]
rkeys = rloc[1]
else:
rowkey = rloc
rkeys = None
if len(rowkey) == 0:
raise KeyError(key)
k = key[1]
if k is None:
colkey = Range(0, self.shape[1] - 1, 1)
else:
if isinstance(k, slice):
sidx = 0 if k.start is None else self.columns.indexOfName(k.start)
if sidx < 0:
raise KeyError(key)
eidx = self.shape[1] - 1 if k.stop is None else self.columns.indexOfName(k.stop)
if eidx < 0:
raise KeyError(key)
step = 1 if k.step is None else k.step
colkey = Range(sidx, eidx, step)
elif isinstance(k, list):
colkey = self.columns.indexOfName(k)
elif isinstance(k, basestring):
col = self.columns.indexOfName(k)
if col < 0:
raise KeyError(key)
colkey = [col]
else:
raise KeyError(key)
if isinstance(rowkey, (int, Range)):
r = self._dataframe.setValues(rowkey, colkey, value)
else:
if isinstance(colkey, Range):
ncol = colkey.length()
else:
ncol = len(colkey)
if len(rowkey) == 1 and ncol == 1:
if isinstance(colkey, Range):
self._dataframe.setValue(rowkey[0], colkey.first(), value)
else:
self._dataframe.setValue(rowkey[0], colkey[0], value)
else:
r = self._dataframe.setValues(rowkey, colkey, value)
def _getitem_iloc(self, key): def _getitem_iloc(self, key):
if not isinstance(key, tuple): if not isinstance(key, tuple):
@ -742,6 +801,7 @@ class DataFrame(object):
:param to_replace: (*object*) The value to be replaced. :param to_replace: (*object*) The value to be replaced.
:param value: (*object*) The replacing value. :param value: (*object*) The replacing value.
:return: (*DataFrame*) New data frame with after value replaced. :return: (*DataFrame*) New data frame with after value replaced.
""" """
r = self._dataframe.replace(to_replace, value) r = self._dataframe.replace(to_replace, value)

View File

@ -14,6 +14,7 @@ import numbers
import mipylib.numeric as np import mipylib.numeric as np
import mipylib.miutil as miutil import mipylib.miutil as miutil
import series
class Index(object): class Index(object):
@ -113,6 +114,9 @@ class Index(object):
:returns: int if unique index, slice if monotonic index, else mask. :returns: int if unique index, slice if monotonic index, else mask.
""" """
if isinstance(key, series.Series):
key = key.values
if isinstance(key, np.NDArray) and key.dtype == np.dtype.bool: if isinstance(key, np.NDArray) and key.dtype == np.dtype.bool:
r = self._index.filterIndices(key.asarray()) r = self._index.filterIndices(key.asarray())
return list(r) return list(r)

View File

@ -2322,32 +2322,6 @@ def meshgrid(*args):
rs.append(NDArray(r)) rs.append(NDArray(r))
return tuple(rs) return tuple(rs)
def meshgrid_bak(*args):
"""
Return coordinate matrices from coordinate vectors.
Make N-D coordinate arrays for vectorized evaluations of N-D scalar/vector fields
over N-D grids, given one-dimensional coordinate arrays x1, x2,, xn.
:param x1,x2...xn: (*array_like*) 1-D arrays representing the coordinates of a grid..
:returns X1,X2...XN: For vectors x1, x2,, xn with lengths Ni=len(xi) ,
return (N1, N2, N3,...Nn) shaped arrays
"""
if isinstance(x, list):
x = array(x)
if isinstance(y, list):
y = array(y)
if x.ndim != 1 or y.ndim != 1:
print 'The paramters must be vector arrays!'
return None
xa = x.asarray()
ya = y.asarray()
ra = ArrayUtil.meshgrid(xa, ya)
return NDArray(ra[0]), NDArray(ra[1])
def sphere(n=20, radius=1): def sphere(n=20, radius=1):
""" """
Create sphere surface coordinate x,y,z array with a radius equal to 1. Create sphere surface coordinate x,y,z array with a radius equal to 1.

View File

@ -8599,14 +8599,14 @@ public class ArrayMath {
public static void replaceValue(Array a, Object oValue, Object value) { public static void replaceValue(Array a, Object oValue, Object value) {
if (a.getIndexPrivate().isFastIterator()) { if (a.getIndexPrivate().isFastIterator()) {
for (int i = 0; i < a.getSize(); i++) { for (int i = 0; i < a.getSize(); i++) {
if (a.getObject(i) == oValue) { if (oValue.equals(a.getObject(i))) {
a.setObject(i, value); a.setObject(i, value);
} }
} }
} else { } else {
IndexIterator iterA = a.getIndexIterator(); IndexIterator iterA = a.getIndexIterator();
while (iterA.hasNext()) { while (iterA.hasNext()) {
if (iterA.getObjectNext() == oValue) { if (oValue.equals(iterA.getObjectNext())) {
iterA.setObjectCurrent(value); iterA.setObjectCurrent(value);
} else { } else {
iterA.next(); iterA.next();