@@ -1063,6 +1063,51 @@ describe("DataFrame", function () {
10631063 } ) ;
10641064 } ) ;
10651065
1066+ describe ( "diff" , function ( ) {
1067+ it ( "Return same DataFrame if other === 0" , function ( ) {
1068+ const data = [ [ 0 , 2 , 4 ] , [ 10 , 10 , 10 ] , [ 1 , 2 , 3 ] ] ;
1069+ const df = new dfd . DataFrame ( data ) ;
1070+ assert . deepEqual ( ( df . diff ( 0 ) ) . values , [ [ 0 , 2 , 4 ] , [ 10 , 10 , 10 ] , [ 1 , 2 , 3 ] ] ) ;
1071+ } ) ;
1072+ it ( "Return difference of DataFrame with previous row" , function ( ) {
1073+ const data = [ [ 0 , 2 , 4 ] , [ 10 , 10 , 10 ] , [ 1 , 2 , 3 ] ] ;
1074+ const df = new dfd . DataFrame ( data ) ;
1075+ assert . deepEqual ( ( df . diff ( 1 ) ) . values , [ [ NaN , NaN , NaN ] , [ 10 , 8 , 6 ] , [ - 9 , - 8 , - 7 ] ] ) ;
1076+ } ) ;
1077+ it ( "Return difference of DataFrame with following row" , function ( ) {
1078+ const data = [ [ 0 , 2 , 4 ] , [ 10 , 10 , 10 ] , [ 1 , 2 , 3 ] ] ;
1079+ const df = new dfd . DataFrame ( data ) ;
1080+ assert . deepEqual ( ( df . diff ( - 1 ) ) . values , [ [ - 10 , - 8 , - 6 ] , [ 9 , 8 , 7 ] , [ NaN , NaN , NaN ] ] ) ;
1081+ } ) ;
1082+ it ( "Return difference of a DataFrame with a Series along default axis 1" , function ( ) {
1083+ const data = [ [ 0 , 2 , 4 ] , [ 10 , 10 , 10 ] , [ 1 , 2 , 3 ] ] ;
1084+ const sf = new dfd . Series ( [ 1 , 2 , 1 ] ) ;
1085+ const df = new dfd . DataFrame ( data ) ;
1086+ assert . deepEqual ( ( df . diff ( sf ) ) . values , [ [ - 1 , 0 , 3 ] , [ 9 , 8 , 9 ] , [ 0 , 0 , 2 ] ] ) ;
1087+ } ) ;
1088+ it ( "Return difference of a DataFrame with along axis 0 (column-wise), previous column" , function ( ) {
1089+ const data = [ [ 0 , 2 , 4 ] , [ 10 , 10 , 10 ] , [ 1 , 2 , 3 ] ] ;
1090+ const df = new dfd . DataFrame ( data ) ;
1091+ assert . deepEqual ( ( df . diff ( 1 , { axis : 0 } ) ) . values , [ [ NaN , 2 , 2 ] , [ NaN , 0 , 0 ] , [ NaN , 1 , 1 ] ] ) ;
1092+ } ) ;
1093+ it ( "Return difference of a DataFrame with along axis 0 (column-wise), following column" , function ( ) {
1094+ const data = [ [ 0 , 2 , 4 ] , [ 10 , 10 , 10 ] , [ 1 , 2 , 3 ] ] ;
1095+ const df = new dfd . DataFrame ( data ) ;
1096+ assert . deepEqual ( ( df . diff ( - 1 , { axis : 0 } ) ) . values , [ [ - 2 , - 2 , NaN ] , [ 0 , 0 , NaN ] , [ - 1 , - 1 , NaN ] ] ) ;
1097+ } ) ;
1098+ it ( "Return difference of a DataFrame with another DataFrame along default axis 1" , function ( ) {
1099+ const df1 = new dfd . DataFrame ( [ [ 0 , 2 , 4 ] , [ 3 , 10 , 4 ] ] ) ;
1100+ const df2 = new dfd . DataFrame ( [ [ - 1 , - 2 , 4 ] , [ 10 , 5 , 0 ] ] ) ;
1101+ assert . deepEqual ( ( df1 . diff ( df2 ) ) . values , [ [ 1 , 4 , 0 ] , [ - 7 , 5 , 4 ] ] ) ;
1102+ } ) ;
1103+ it ( "Throw error if DataFrame for diff contains string" , function ( ) {
1104+ const df = new dfd . DataFrame ( [ [ "words" , "words" , "words" ] , [ "words" , "words" , "words" ] ] ) ;
1105+ assert . throws ( ( ) => {
1106+ df . diff ( 1 ) ;
1107+ } , Error , "TypeError: diff operation is not supported for string dtypes" ) ;
1108+ } ) ;
1109+ } ) ;
1110+
10661111 describe ( "mean" , function ( ) {
10671112 it ( "Returns the mean of a DataFrame (Default axis is [1:column])" , function ( ) {
10681113 const data = [ [ 0 , 2 , 4 ] ,
0 commit comments