21 using System.Collections;
22 using System.Collections.Generic;
32 private static dynamic _pandas;
33 private static PyObject _concat;
42 var pandas = Py.Import(
"pandas");
45 _concat = pandas.GetAttr(
"concat");
57 public PyObject
GetDataFrame(IEnumerable<Slice> data,
bool flatten =
false, Type dataType =
null)
59 var generator =
new DataFrameGenerator(data, flatten, dataType);
60 return generator.GenerateDataFrame();
74 public PyObject
GetDataFrame<T>(IEnumerable<T> data,
bool symbolOnlyIndex =
false,
bool forceMultiValueSymbol =
false,
bool flatten =
false)
77 var generator =
new DataFrameGenerator<T>(data, flatten);
78 return generator.GenerateDataFrame(
80 levels: symbolOnlyIndex ? 1 : 2,
82 symbolOnlyIndex: symbolOnlyIndex,
83 forceMultiValueSymbol: forceMultiValueSymbol);
95 using var pyDict =
new PyDict();
97 foreach (var kvp
in data)
99 AddSeriesToPyDict(kvp.Key, kvp.Value, pyDict);
102 return MakeIndicatorDataFrame(pyDict);
115 using var inputPythonType = data.GetPythonType();
116 var inputTypeStr = inputPythonType.ToString();
117 var targetTypeStr = nameof(PyDict);
118 PyObject currentKvp =
null;
122 using var pyDictData =
new PyDict(data);
123 using var seriesPyDict =
new PyDict();
125 targetTypeStr = $
"{nameof(String)}: {nameof(List<IndicatorDataPoint>)}";
127 foreach (var kvp
in pyDictData.Items())
130 AddSeriesToPyDict(kvp[0].As<string>(), kvp[1].As<List<IndicatorDataPoint>>(), seriesPyDict);
133 return MakeIndicatorDataFrame(seriesPyDict);
137 if (currentKvp !=
null)
139 inputTypeStr = $
"{currentKvp[0].GetPythonType()}: {currentKvp[1].GetPythonType()}";
160 return _pandas.Repr();
176 public static PyObject
ConcatDataFrames<T>(IEnumerable<PyObject> dataFrames, IEnumerable<T> keys, IEnumerable<string> names,
177 bool sort =
true,
bool dropna =
true)
181 using var pyDataFrames = dataFrames.ToPyListUnSafe();
183 if (pyDataFrames.Length() == 0)
185 return _pandas.DataFrame();
188 using var kwargs = Py.kw(
"sort", sort);
189 PyList pyKeys =
null;
190 PyList pyNames =
null;
194 if (keys !=
null && names !=
null)
196 pyNames = names.ToPyListUnSafe();
197 pyKeys = ConvertConcatKeys(keys);
198 using var pyFalse =
false.ToPython();
200 kwargs.SetItem(
"keys", pyKeys);
201 kwargs.SetItem(
"names", pyNames);
202 kwargs.SetItem(
"copy", pyFalse);
205 var result = _concat.Invoke(
new[] { pyDataFrames }, kwargs);
210 using var dropnaKwargs = Py.kw(
"axis", 1,
"inplace",
true,
"how",
"all");
211 result.GetAttr(
"dropna").Invoke(Array.Empty<PyObject>(), dropnaKwargs);
224 public static PyObject ConcatDataFrames(IEnumerable<PyObject> dataFrames,
bool sort =
true,
bool dropna =
true)
226 return ConcatDataFrames<string>(dataFrames,
null,
null, sort, dropna);
233 private static PyList ConvertConcatKeys(IEnumerable<IEnumerable<object>> keys)
235 var keyTuples = keys.Select(x =>
new PyTuple(x.Select(y => y.ToPython()).ToArray()));
238 return keyTuples.ToPyListUnSafe();
242 foreach (var tuple
in keyTuples)
244 foreach (var x
in tuple)
248 tuple.DisposeSafely();
253 private static PyList ConvertConcatKeys<T>(IEnumerable<T> keys)
255 if ((typeof(T).IsAssignableTo(typeof(IEnumerable)) && !typeof(T).IsAssignableTo(typeof(
string))))
257 return ConvertConcatKeys(keys.Cast<IEnumerable<object>>());
260 return keys.ToPyListUnSafe();
270 private void AddSeriesToPyDict(
string key, List<IndicatorDataPoint> points, PyDict pyDict)
272 var index =
new List<DateTime>();
273 var values =
new List<double>();
275 foreach (var point
in points)
277 index.Add(point.EndTime);
278 values.Add((
double) point.Value);
280 pyDict.SetItem(key.ToLowerInvariant(), _pandas.Series(values, index));
288 private PyObject MakeIndicatorDataFrame(PyDict pyDict)
290 return _pandas.DataFrame(pyDict, columns: pyDict.Keys().Select(x => x.As<
string>().ToLowerInvariant()).OrderBy(x => x));