File tree Expand file tree Collapse file tree 1 file changed +12
-3
lines changed Expand file tree Collapse file tree 1 file changed +12
-3
lines changed Original file line number Diff line number Diff line change 1
1
__all__ = ['calc_average_citations' ]
2
2
3
- def calc_average_citations (in_df , sort = False ):
3
+ def calc_average_citations (in_df , max_year = None , sort = False ):
4
4
"""Calculate the average citations since year of publication.
5
5
6
6
Example
@@ -14,7 +14,8 @@ def calc_average_citations(in_df, sort=False):
14
14
15
15
Parameters
16
16
==========
17
- * df : Pandas DataFrame
17
+ * in_df : Pandas DataFrame
18
+ * max_year: int or None, year to calculate average citations from.
18
19
19
20
Returns
20
21
==========
@@ -23,7 +24,15 @@ def calc_average_citations(in_df, sort=False):
23
24
assert hasattr (in_df , 'citations' ), \
24
25
'DataFrame has to have `citation` column. Use `get_num_citations()` first'
25
26
out_df = in_df .copy ()
26
- max_year = out_df .year .max ()
27
+
28
+ max_year_in_data = out_df .year .max ()
29
+
30
+ if max_year not None :
31
+ max_year = int (max_year )
32
+ assert max_year_in_data <= max_year , \
33
+ "Given max_year must be later than any year found in dataset."
34
+ else :
35
+ max_year = out_df .year .max ()
27
36
out_df .loc [:, 'Avg. Citations' ] = (out_df .citations / ((max_year - out_df .year ) + 1 )).astype (float ).round (2 )
28
37
29
38
if sort :
You can’t perform that action at this time.
0 commit comments