Skip to content

Commit

Permalink
Allow specification of an anchor point in time (max year)
Browse files Browse the repository at this point in the history
  • Loading branch information
ConnectedSystems committed May 1, 2019
1 parent a992186 commit 57df14a
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions wosis/analysis/stats.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
__all__ = ['calc_average_citations']

def calc_average_citations(in_df, sort=False):
def calc_average_citations(in_df, max_year=None, sort=False):
"""Calculate the average citations since year of publication.
Example
Expand All @@ -14,7 +14,8 @@ def calc_average_citations(in_df, sort=False):
Parameters
==========
* df : Pandas DataFrame
* in_df : Pandas DataFrame
* max_year: int or None, year to calculate average citations from.
Returns
==========
Expand All @@ -23,7 +24,15 @@ def calc_average_citations(in_df, sort=False):
assert hasattr(in_df, 'citations'), \
'DataFrame has to have `citation` column. Use `get_num_citations()` first'
out_df = in_df.copy()
max_year = out_df.year.max()

max_year_in_data = out_df.year.max()

if max_year not None:
max_year = int(max_year)
assert max_year_in_data <= max_year, \
"Given max_year must be later than any year found in dataset."
else:
max_year = out_df.year.max()
out_df.loc[:, 'Avg. Citations'] = (out_df.citations / ((max_year - out_df.year) + 1)).astype(float).round(2)

if sort:
Expand Down

0 comments on commit 57df14a

Please sign in to comment.