/// <summary> /// 切片 /// </summary> /// <param name="s"></param> /// <returns></returns> public IDataFrame this[SliceLabel s] { get { List <object> objs = new List <object>(); List <TIndex> indexs = null; if (_rawIndex != null) { indexs = new List <TIndex>(); } NDArray nDArray = null; //转换获取的标签行索引 int start = 0; int end = int.MaxValue; if (!string.IsNullOrEmpty(s.StartLabel)) { start = Index.GetPosition(s.StartLabel); } if (!string.IsNullOrEmpty(s.EndLabel)) { end = Index.GetPosition(s.EndLabel); } //验证长度 var lengthVail = Values.size / Values.shape[1] - 1; var length = end; if (end > lengthVail) { length = lengthVail; } //验证开始和结束位置 VailStartAndEnd(start, length, lengthVail); bool desc = s.Step < 0; //修正start和length,降序 if (desc) { int temp = start; start = length - 1; length = temp; } for (int i = start; Calc(i, length, desc, true); i += s.Step) { //获取下标 int index = GetIndex(i, lengthVail); if (_rawIndex != null) { indexs.Add(_rawIndex[index]); } var value = Values[index] as NDArray; objs.AddRange(value.Data <object>()); } nDArray = np.array(objs.ToArray(), Values.dtype); int ndim = Columns.Size; nDArray.reshape(objs.Count / ndim, ndim); var result = new DataFrame <TIndex>(nDArray, indexs, _rawColumns, _dtype); return(result); } }
public void Create_implicit() { SliceLabel sliceLabel = "row1:row2:3"; Assert.Equal("row1", sliceLabel.StartLabel); Assert.Equal("row2", sliceLabel.EndLabel); Assert.Equal(3, sliceLabel.Step); }