@@ -992,6 +992,51 @@ describe("DataFrame", function () {
992992
993993 } ) ;
994994
995+ describe ( "diff" , function ( ) {
996+ it ( "Return same DataFrame if other === 0" , function ( ) {
997+ const data = [ [ 0 , 2 , 4 ] , [ 10 , 10 , 10 ] , [ 1 , 2 , 3 ] ] ;
998+ const df = new DataFrame ( data ) ;
999+ assert . deepEqual ( ( df . diff ( 0 ) as DataFrame ) . values , [ [ 0 , 2 , 4 ] , [ 10 , 10 , 10 ] , [ 1 , 2 , 3 ] ] ) ;
1000+ } ) ;
1001+ it ( "Return difference of DataFrame with previous row" , function ( ) {
1002+ const data = [ [ 0 , 2 , 4 ] , [ 10 , 10 , 10 ] , [ 1 , 2 , 3 ] ] ;
1003+ const df = new DataFrame ( data ) ;
1004+ assert . deepEqual ( ( df . diff ( 1 ) as DataFrame ) . values , [ [ NaN , NaN , NaN ] , [ 10 , 8 , 6 ] , [ - 9 , - 8 , - 7 ] ] ) ;
1005+ } ) ;
1006+ it ( "Return difference of DataFrame with following row" , function ( ) {
1007+ const data = [ [ 0 , 2 , 4 ] , [ 10 , 10 , 10 ] , [ 1 , 2 , 3 ] ] ;
1008+ const df = new DataFrame ( data ) ;
1009+ assert . deepEqual ( ( df . diff ( - 1 ) as DataFrame ) . values , [ [ - 10 , - 8 , - 6 ] , [ 9 , 8 , 7 ] , [ NaN , NaN , NaN ] ] ) ;
1010+ } ) ;
1011+ it ( "Return difference of a DataFrame with a Series along default axis 1" , function ( ) {
1012+ const data = [ [ 0 , 2 , 4 ] , [ 10 , 10 , 10 ] , [ 1 , 2 , 3 ] ] ;
1013+ const sf = new Series ( [ 1 , 2 , 1 ] ) ;
1014+ const df = new DataFrame ( data ) ;
1015+ assert . deepEqual ( ( df . diff ( sf ) as DataFrame ) . values , [ [ - 1 , 0 , 3 ] , [ 9 , 8 , 9 ] , [ 0 , 0 , 2 ] ] ) ;
1016+ } ) ;
1017+ it ( "Return difference of a DataFrame with along axis 0 (column-wise), previous column" , function ( ) {
1018+ const data = [ [ 0 , 2 , 4 ] , [ 10 , 10 , 10 ] , [ 1 , 2 , 3 ] ] ;
1019+ const df = new DataFrame ( data ) ;
1020+ assert . deepEqual ( ( df . diff ( 1 , { axis : 0 } ) as DataFrame ) . values , [ [ NaN , 2 , 2 ] , [ NaN , 0 , 0 ] , [ NaN , 1 , 1 ] ] ) ;
1021+ } ) ;
1022+ it ( "Return difference of a DataFrame with along axis 0 (column-wise), following column" , function ( ) {
1023+ const data = [ [ 0 , 2 , 4 ] , [ 10 , 10 , 10 ] , [ 1 , 2 , 3 ] ] ;
1024+ const df = new DataFrame ( data ) ;
1025+ assert . deepEqual ( ( df . diff ( - 1 , { axis : 0 } ) as DataFrame ) . values , [ [ - 2 , - 2 , NaN ] , [ 0 , 0 , NaN ] , [ - 1 , - 1 , NaN ] ] ) ;
1026+ } ) ;
1027+ it ( "Return difference of a DataFrame with another DataFrame along default axis 1" , function ( ) {
1028+ const df1 = new DataFrame ( [ [ 0 , 2 , 4 ] , [ 3 , 10 , 4 ] ] ) ;
1029+ const df2 = new DataFrame ( [ [ - 1 , - 2 , 4 ] , [ 10 , 5 , 0 ] ] ) ;
1030+ assert . deepEqual ( ( df1 . diff ( df2 ) as DataFrame ) . values , [ [ 1 , 4 , 0 ] , [ - 7 , 5 , 4 ] ] ) ;
1031+ } ) ;
1032+ it ( "Throw error if DataFrame for diff contains string" , function ( ) {
1033+ const df = new DataFrame ( [ [ "words" , "words" , "words" ] , [ "words" , "words" , "words" ] ] ) ;
1034+ assert . throws ( ( ) => {
1035+ df . diff ( 1 ) ;
1036+ } , Error , "TypeError: diff operation is not supported for string dtypes" ) ;
1037+ } ) ;
1038+ } ) ;
1039+
9951040 describe ( "mod" , function ( ) {
9961041 it ( "Return modulus of DataFrame with a single Number" , function ( ) {
9971042 const data = [ [ 0 , 2 , 4 ] , [ 360 , 180 , 360 ] ] ;
0 commit comments