Procházet zdrojové kódy

Finish the DataFrame Index

Huan před 5 roky
rodič
revize
54f85be07a

+ 6 - 0
.idea/vcs.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="$PROJECT_DIR$" vcs="Git" />
+  </component>
+</project>

binární
CGB/__pycache__/TK.cpython-37.pyc


binární
CGB/__pycache__/TK_DoneHS.cpython-37.pyc


binární
CGB/__pycache__/TK_HS.cpython-37.pyc


binární
CGB/__pycache__/Write.cpython-37.pyc


binární
CGB/__pycache__/__init__.cpython-37.pyc


binární
DSGC/__pycache__/Algebra.cpython-37.pyc


binární
DSGC/__pycache__/Algebra_Systemctl.cpython-37.pyc


binární
DSGC/__pycache__/__init__.cpython-37.pyc


binární
HSCH/__pycache__/Func_Matlib.cpython-37.pyc


binární
HSCH/__pycache__/Func_advanced.cpython-37.pyc


binární
HSCH/__pycache__/HS.cpython-37.pyc


binární
HSCH/__pycache__/__init__.cpython-37.pyc


+ 107 - 14
Learn.py

@@ -46,7 +46,7 @@ class Form:
     def __Add_Form(self,Dic,Func,name='',Index=True,**kwargs):#新增表格的核心方式
         try:
             Data = Func(Dic,**kwargs)
-        except UnicodeDecodeError:
+        except UnicodeDecodeError:#找不到编码方式
             return False
         if not Index:
             Data.index = Data.iloc[:,0].tolist()
@@ -60,19 +60,21 @@ class Form:
             k = {}
         return self.__Add_Form(Dic,pd.read_csv,name,Index,sep=Sep,encoding=code,**k)
 
-    def Add_Python(self,Text,sheet_name=''):
-        name = {}
+    def Add_Python(self,Text,sheet_name='') -> pd.DataFrame:
+        name = {'Sheet':self.get_Sheet}
         name.update(globals().copy())
         name.update(locals().copy())
         exec(Text,name)
         exec('get = Creat()',name)
-        if isinstance(name['get'],pd.DataFrame):
-            self.Add_Form(name['get'],sheet_name)
+        if isinstance(name['get'],pd.DataFrame):#已经是DataFram
+            get = name['get']
         else:
             try:
                 get = pd.DataFrame(name['get'])
-                self.Add_Form(get, sheet_name)
-            except:pass
+            except:
+                get = pd.DataFrame([name['get']])
+        self.Add_Form(get, sheet_name)
+        return get
 
     def Add_Html(self,Dic,name='',code='utf-8',str_=True,Index=True):
         if str_:
@@ -144,7 +146,6 @@ class Form:
         else:
             re = []
             loc_list = get.columns.values
-            print(loc_list)
             a = 0
             for i in loc_list:
                 data = get[i].to_list()
@@ -159,7 +160,6 @@ class Form:
         else:
             re = []
             loc_list = get.index.values
-            print(loc_list)
             a = 0
             for i in range(len(loc_list)):
                 l = loc_list[i]
@@ -300,7 +300,7 @@ class Form:
                 for c in Done_Column:
                     try:
                         print(f'r={r},c={c}')
-                        n = eval(f"get.iloc[{r},{c}]")#第一个是行名,然后是列名
+                        n = eval(f"get.iloc[{r},{c}]")#第一个是行号,然后是列号
                         r_h = eval(f"get.iloc[{r}]")
                         c_h = eval(f"get.iloc[:,{c}]")
                         print(f'n={n}')
@@ -1016,7 +1016,7 @@ class Form:
                 column[i] = int(column[i])
             except:
                 pass
-        func_Dic = {'Int': int, 'Float': float, 'Str':str}
+        func_Dic = {'Int': int, 'Float': float, 'Str':str,'Date':pd.Timestamp,'TimeDelta':pd.Timedelta}
         if column != []:
             get.iloc[:, column] = get.iloc[:, column].astype(func_Dic.get(dtype,dtype),errors=wrong)
             print('A')
@@ -1025,14 +1025,107 @@ class Form:
         self.Add_Form(get)
         return get
 
-    def Replace(self,name,is_column,Dic):
+    def Replace_Index(self,name,is_column,Dic,save):
         get = self.get_Sheet(name)
         if is_column:
+            if save:#保存原数据
+                get.loc['column'] = self.get_Column(name, True)
             new = get.rename(columns=Dic)
         else:
+            if save:
+                get.loc[:, 'row'] = self.get_Index(name, True)
             new = get.rename(index=Dic)
         self.Add_Form(new)
         return new
 
-    def Replace_ByList(self,name,is_column,iloc):
-        pass
+    def Change_Index(self,name:str,is_column:bool,iloc:int,save:bool=True,drop:bool=False):
+        get = self.get_Sheet(name).copy()
+        if is_column:#列名
+            Col = self.get_Column(name, True)
+            t = Col.tolist()[slice(iloc,iloc+1)]
+            if save:#保存原数据
+                get.loc['column'] = Col
+            get.columns = get.loc[t].values[0] #调整 loc[t]取行数据,否则为DataFrame,不可做为Index
+            if drop:
+                get.drop(t,axis=0,inplace=True)#删除行
+        else:
+            Row = self.get_Index(name, True)
+            t = Row.tolist()[slice(iloc, iloc + 1)]
+            if save:
+                get.loc[:, 'row'] = Row
+            new_index_list = get.loc[:,t].values
+            new_index = []
+            for i in new_index_list:
+                new_index.append(i[0])
+            print(new_index)
+            get.index = new_index  # 调整
+            if drop:
+                get.drop(t,axis=1,inplace=True)#删除行
+        self.Add_Form(get)
+        return get
+
+    def num_toName(self,name,is_column,save):
+        get = self.get_Sheet(name).copy()
+        if is_column:#处理列名
+            Col = self.get_Column(name,True)
+            if save:#保存原数据
+                get.loc['column'] = Col
+            get.columns = [i for i in range(len(Col))]
+        else:
+            Row = self.get_Index(name,True)
+            if save:
+                get.loc[:, 'row'] = Row
+            get.index = [i for i in range(len(Row))]
+        self.Add_Form(get)
+        return get
+
+    def num_withName(self,name,is_column,save):
+        get = self.get_Sheet(name).copy()
+        if is_column:#处理列名
+            Col = self.get_Column(name,True)
+            if save:#保存原数据
+                get.loc['column'] = Col
+            get.columns = [f'[{i}]{Col[i]}' for i in range(len(Col))]
+        else:
+            Row = self.get_Index(name,True)
+            if save:
+                get.loc[:, 'row'] = Row
+            get.index = [f'[{i}]{Row[i]}' for i in range(len(Row))]
+        self.Add_Form(get)
+        return get
+
+    def Date_Index(self,name,is_column,save,**Date_Init):
+        #Date_Init:start,end,freq 任意两样
+        get = self.get_Sheet(name)
+        if is_column:  # 处理列名
+            Col = self.get_Column(name, True)
+            if save:  # 保存原数据
+                get.loc['column'] = Col
+            Date_Init['periods'] = len(Col)
+            get.columns = pd.date_range(**Date_Init)
+        else:
+            Row = self.get_Index(name, True)
+            if save:
+                get.loc[:, 'row'] = Row
+            Date_Init['periods'] = len(Row)
+            get.index = pd.date_range(**Date_Init)
+        self.Add_Form(get)
+        return get
+
+    def Time_Index(self,name,is_column,save,**Time_Init):
+        #Date_Init:start,end,freq 任意两样
+        get = self.get_Sheet(name)
+        if is_column:  # 处理列名
+            Col = self.get_Column(name, True)
+            if save:  # 保存原数据
+                get.loc['column'] = Col
+            Time_Init['periods'] = len(Col)
+            get.columns = pd.timedelta_range(**Time_Init)
+        else:
+            Row = self.get_Index(name, True)
+            if save:
+                get.loc[:, 'row'] = Row
+            Time_Init['periods'] = len(Row)
+            get.index = pd.timedelta_range(**Time_Init)
+        self.Add_Form(get)
+        return get

+ 81 - 25
Me_learn.py

@@ -919,21 +919,25 @@ def Machine_learning():
     tkinter.Label(top, text='【行名与列名】', bg=bg, fg=fg, font=FONT, width=width_B*3, height=height_B).grid(column=a_x,
                                                                                                columnspan=3,row=a_y, sticky=tkinter.E + tkinter.W + tkinter.W+tkinter.S + tkinter.N)  # 设置说明
 
-    global replace_Dic,Repalce_RC,replace_List,Date_Inpue,RC_Type
+    global replace_Dic,Repalce_RC,replace_iloc,Date_Input,RC_Type
     a_y += 1
     Repalce_RC = tkinter.IntVar()
-    lable = ['操作行','操作列']#复选框
+    lable = ['(列数据)调整行名','(行数据)调整列名']#复选框
     for i in range(2):
         tkinter.Radiobutton(top,bg = bg,fg = fg,activebackground=bg,activeforeground=fg,selectcolor=bg,text=lable[i],
                             variable=Repalce_RC, value=i).grid(column=a_x+i, row=a_y, sticky=tkinter.W)
+    tkinter.Button(top, bg=bbg, fg=fg, text='植入行(列)号',command=num_withName, font=FONT, width=width_B,height=height_B).\
+        grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
 
     a_y += 1
     RC_Type = []
-    lable = ['保留原值','保留新值','不刷入新值']#复选框
-    for i in range(3):
+    lable = ['保留原值','保留新值']#复选框
+    for i in range(2):
         RC_Type.append(tkinter.IntVar())
         tkinter.Checkbutton(top,bg = bg,fg = fg,activebackground=bg,activeforeground=fg,selectcolor=bg, text=lable[i],
                             variable=RC_Type[-1]).grid(column=a_x+i, row=a_y, sticky=tkinter.W)
+    tkinter.Button(top, bg=bbg, fg=fg, text='统一行号',command=num_toName, font=FONT, width=width_B,height=height_B).\
+        grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
 
     a_y += 1
     tkinter.Label(top, text='替换字典:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)  # 设置说明
@@ -942,29 +946,29 @@ def Machine_learning():
 
     a_y += 1
     tkinter.Label(top, text='替换列(行):', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)  # 设置说明
-    replace_List = tkinter.Entry(top, width=width_B * 2)
-    replace_List.grid(column=a_x + 1, row=a_y, columnspan=2, sticky=tkinter.E + tkinter.W)
+    replace_iloc = tkinter.Entry(top, width=width_B * 2)
+    replace_iloc.grid(column=a_x + 1, row=a_y, columnspan=2, sticky=tkinter.E + tkinter.W)
 
     a_y += 1
-    tkinter.Button(top, bg=bbg, fg=fg, text='执行替换已有列(行)操作', font=FONT, width=width_B*2, height=height_B). \
+    tkinter.Button(top, bg=bbg, fg=fg, text='执行替换已有列(行)操作',command=Change_Index, font=FONT, width=width_B*2, height=height_B). \
         grid(column=a_x,columnspan=2, row=a_y, sticky=tkinter.E + tkinter.W)
-    tkinter.Button(top, bg=bbg, fg=fg, text='执行替换操作',command=Replace, font=FONT, width=width_B,height=height_B).\
+    tkinter.Button(top, bg=bbg, fg=fg, text='执行替换操作',command=Replace_Index, font=FONT, width=width_B,height=height_B).\
         grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
 
-    label = ['起点','终点','长度','间隔']
-    Date_Inpue = []
-    for i in range(4):
+    label = ['起点','终点','间隔']
+    Date_Input = []
+    for i in range(3):
         a_y += 1
         tkinter.Label(top, text='时间序列'+label[i], bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)  # 设置说明
-        Date_Inpue.append(tkinter.Entry(top, width=width_B * 2))
-        Date_Inpue[-1].grid(column=a_x + 1, row=a_y, columnspan=2, sticky=tkinter.E + tkinter.W)
+        Date_Input.append(tkinter.Entry(top, width=width_B * 2))
+        Date_Input[-1].grid(column=a_x + 1, row=a_y, columnspan=2, sticky=tkinter.E + tkinter.W)
 
     global Date_Type
     a_y += 1
     Date_Type = tkinter.IntVar()
-    tkinter.Button(top, bg=bbg, fg=fg, text='刷入Date序列', font=FONT, width=width_B,height=height_B).\
+    tkinter.Button(top, bg=bbg, fg=fg, text='刷入Date序列',command=Date_Index, font=FONT, width=width_B,height=height_B).\
         grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
-    tkinter.Button(top, bg=bbg, fg=fg, text='刷入Time序列', font=FONT, width=width_B, height=height_B). \
+    tkinter.Button(top, bg=bbg, fg=fg, text='刷入Time序列',command=Time_index, font=FONT, width=width_B, height=height_B). \
         grid(column=a_x+1, row=a_y, sticky=tkinter.E + tkinter.W)
     tkinter.Checkbutton(top, bg=bg, fg=fg, activebackground=bg, activeforeground=fg, selectcolor=bg, text='使用间隔',
                         variable=Date_Type).grid(column=a_x + 2, row=a_y, sticky=tkinter.W)
@@ -973,21 +977,73 @@ def Machine_learning():
 
 #出现在此下面的函数应转移到上方方便管理!......
 
-def Replace():
-    global replace_Dic, Repalce_RC, replace_List, Replace_Split, Date_Inpue, RC_Type,ML
+def Replace_Index():
+    global replace_Dic, Repalce_RC,ML
+    name = get_Name()
     Dic = eval(replace_Dic.get())
     is_Column = bool(Repalce_RC.get()) #操作行-False,操作列-True
-    name = get_Name()
-    ML.Replace(name,is_Column,Dic)
+    save = bool(RC_Type[0].get())
+
+    ML.Replace_Index(name,is_Column,Dic,save)
     Updat_BOX()
 
-def Replace_BySplit():
-    global replace_Dic, Repalce_RC, replace_List, Replace_Split, Date_Inpue, RC_Type,ML
-    iloc = int(replace_List.get())
-    is_Column = bool(Repalce_RC.get()) #操作行-False,操作列-True
-    name = get_Name()
-    ML.Replace(name,is_Column,iloc)
+def Change_Index():
+    global Repalce_RC, replace_iloc, RC_Type,ML
+    name = get_Name()  # 名字
+    is_Column = bool(Repalce_RC.get())  # 操作行名-False,操作列名-True
+    iloc = int(replace_iloc.get()) # 替换的列号(行号)
+    save = bool(RC_Type[0].get())
+    drop = not bool(RC_Type[1].get())
+
+    ML.Change_Index(name,is_Column,iloc,save,drop)
     Updat_BOX()
 
+def num_toName():
+    global Repalce_RC, replace_iloc, RC_Type,ML
+    name = get_Name()  # 名字
+    is_Column = bool(Repalce_RC.get())  # 操作行名-False,操作列名-True
+    save = bool(RC_Type[0].get())
+
+    ML.num_toName(name,is_Column,save)
+    Updat_BOX()
+
+def num_withName():
+    global Repalce_RC,RC_Type,ML
+    name = get_Name()  # 名字
+    is_Column = bool(Repalce_RC.get())  # 操作行名-False,操作列名-True
+    save = bool(RC_Type[0].get())
+
+    ML.num_withName(name,is_Column,save)
+    Updat_BOX()
+
+def DateTime_Index(is_Date=True):
+    global Repalce_RC, RC_Type,ML,Date_Input,Date_Type
+    name = get_Name()  # 名字
+    is_Column = bool(Repalce_RC.get())  # 操作行名-False,操作列名-True
+    save = bool(RC_Type[0].get())
+
+    k = ['start','end','freq']
+    Init = {}
+    for i in range(len(Date_Input)):
+        Input = Date_Input[i].get()
+        if Input == '':continue
+        Init[k[i]] = Input
+    if len(Init) == 3:
+        if bool(Date_Type.get()):#使用间隔
+            del Init['end']
+        else:
+            del Init['freq']
+    if is_Date:
+        ML.Date_Index(name,is_Column,save,**Init)
+    else:
+        ML.Time_Index(name, is_Column, save, **Init)
+    Updat_BOX()
+
+def Date_Index():
+    DateTime_Index(True)
+
+def Time_index():
+    DateTime_Index(False)
+
 if __name__ == '__main__':
     Machine_learning()

binární
New_TK/__pycache__/__init__.cpython-37.pyc


+ 0 - 5
Test2.py

@@ -1,5 +0,0 @@
-import pandas as pd
-
-df = pd.DataFrame({'a': ['7.2', '1s', '5'], 'b': ['3','2','1']}, dtype='object')
-
-print(df)

binární
__pycache__/Learn.cpython-37.pyc