diff --git a/meteoinfo-dataframe/src/main/java/org/meteoinfo/dataframe/DataFrame.java b/meteoinfo-dataframe/src/main/java/org/meteoinfo/dataframe/DataFrame.java index 701a1756..c4f02d83 100644 --- a/meteoinfo-dataframe/src/main/java/org/meteoinfo/dataframe/DataFrame.java +++ b/meteoinfo-dataframe/src/main/java/org/meteoinfo/dataframe/DataFrame.java @@ -1396,11 +1396,6 @@ public class DataFrame implements Iterable { * @throws org.meteoinfo.ndarray.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) { List ranges = new ArrayList<>(); 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) 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 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) 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 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) 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 rowRange, List 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) 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 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) 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 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) 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 rowRange, List 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) this.data).get(j); + for (int i : rowRange) { + array.setObject(i, value.getObject(idx)); + idx += 1; + } + } + } + } + /** * Set values by row and column ranges * @@ -1422,6 +1597,8 @@ public class DataFrame implements Iterable { * @throws org.meteoinfo.ndarray.InvalidRangeException */ public void setValues(int row, 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()); @@ -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) 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 * diff --git a/meteoinfo-lab/milconfig.xml b/meteoinfo-lab/milconfig.xml index 3b1a8e2d..2ba1c901 100644 --- a/meteoinfo-lab/milconfig.xml +++ b/meteoinfo-lab/milconfig.xml @@ -1,7 +1,6 @@ - - + @@ -10,27 +9,28 @@ - + + - - - + + + - - - + + + @@ -38,5 +38,5 @@
- + diff --git a/meteoinfo-lab/pylib/mipylib/dataframe/dataframe$py.class b/meteoinfo-lab/pylib/mipylib/dataframe/dataframe$py.class index 08c6e3fd..ae3c9513 100644 Binary files a/meteoinfo-lab/pylib/mipylib/dataframe/dataframe$py.class and b/meteoinfo-lab/pylib/mipylib/dataframe/dataframe$py.class differ diff --git a/meteoinfo-lab/pylib/mipylib/dataframe/dataframe.py b/meteoinfo-lab/pylib/mipylib/dataframe/dataframe.py index deed7449..83f995fb 100644 --- a/meteoinfo-lab/pylib/mipylib/dataframe/dataframe.py +++ b/meteoinfo-lab/pylib/mipylib/dataframe/dataframe.py @@ -478,15 +478,74 @@ class DataFrame(object): def _setitem_loc(self, key, value): if isinstance(value, datetime.datetime): - value = [miutil.jdatetime(value)] + value = miutil.jdatetime(value) if isinstance(value, (list, tuple)): - if isinstance(value[0], datetime.datetime): - value = miutil.jdatetime(value) - #value = np.array(value) + value = np.array(value) if isinstance(value, np.NDArray): - value = value._array - - self._dataframe.setRow(key, value) + value = value._array + + 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): if not isinstance(key, tuple): @@ -742,6 +801,7 @@ class DataFrame(object): :param to_replace: (*object*) The value to be replaced. :param value: (*object*) The replacing value. + :return: (*DataFrame*) New data frame with after value replaced. """ r = self._dataframe.replace(to_replace, value) diff --git a/meteoinfo-lab/pylib/mipylib/dataframe/index$py.class b/meteoinfo-lab/pylib/mipylib/dataframe/index$py.class index be06d694..ac02151a 100644 Binary files a/meteoinfo-lab/pylib/mipylib/dataframe/index$py.class and b/meteoinfo-lab/pylib/mipylib/dataframe/index$py.class differ diff --git a/meteoinfo-lab/pylib/mipylib/dataframe/index.py b/meteoinfo-lab/pylib/mipylib/dataframe/index.py index 5622c021..f0f8a9cf 100644 --- a/meteoinfo-lab/pylib/mipylib/dataframe/index.py +++ b/meteoinfo-lab/pylib/mipylib/dataframe/index.py @@ -14,6 +14,7 @@ import numbers import mipylib.numeric as np import mipylib.miutil as miutil +import series class Index(object): @@ -113,6 +114,9 @@ class Index(object): :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: r = self._index.filterIndices(key.asarray()) return list(r) diff --git a/meteoinfo-lab/pylib/mipylib/numeric/core/numeric$py.class b/meteoinfo-lab/pylib/mipylib/numeric/core/numeric$py.class index 781d9fe8..8e9bea17 100644 Binary files a/meteoinfo-lab/pylib/mipylib/numeric/core/numeric$py.class and b/meteoinfo-lab/pylib/mipylib/numeric/core/numeric$py.class differ diff --git a/meteoinfo-lab/pylib/mipylib/numeric/core/numeric.py b/meteoinfo-lab/pylib/mipylib/numeric/core/numeric.py index 97981664..89054307 100644 --- a/meteoinfo-lab/pylib/mipylib/numeric/core/numeric.py +++ b/meteoinfo-lab/pylib/mipylib/numeric/core/numeric.py @@ -2322,32 +2322,6 @@ def meshgrid(*args): rs.append(NDArray(r)) 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): """ Create sphere surface coordinate x,y,z array with a radius equal to 1. diff --git a/meteoinfo-ndarray/src/main/java/org/meteoinfo/ndarray/math/ArrayMath.java b/meteoinfo-ndarray/src/main/java/org/meteoinfo/ndarray/math/ArrayMath.java index fc0b89d7..ae9dc75b 100644 --- a/meteoinfo-ndarray/src/main/java/org/meteoinfo/ndarray/math/ArrayMath.java +++ b/meteoinfo-ndarray/src/main/java/org/meteoinfo/ndarray/math/ArrayMath.java @@ -8599,14 +8599,14 @@ public class ArrayMath { public static void replaceValue(Array a, Object oValue, Object value) { if (a.getIndexPrivate().isFastIterator()) { for (int i = 0; i < a.getSize(); i++) { - if (a.getObject(i) == oValue) { + if (oValue.equals(a.getObject(i))) { a.setObject(i, value); } } } else { IndexIterator iterA = a.getIndexIterator(); while (iterA.hasNext()) { - if (iterA.getObjectNext() == oValue) { + if (oValue.equals(iterA.getObjectNext())) { iterA.setObjectCurrent(value); } else { iterA.next();